【发布时间】:2017-03-05 14:51:59
【问题描述】:
#import "Session.h"
#import <MultipeerConnectivity/MultipeerConnectivity.h>
@interface Session () <MCSessionDelegate,MCBrowserViewControllerDelegate>
@property (strong,nonatomic) MCSession *session;
@property (strong,nonatomic) MCAdvertiserAssistant *advertiser;
@property (strong,nonatomic) MCPeerID *peerId;
@end
@implementation Session
#pragma mark - Initialiser(s)
//Setting the name of the peer
-(id)initWithDisplayName:(NSString *)name{
self = [super init];
self.peerId = [[MCPeerID alloc]initWithDisplayName:name];
return self;
}
#pragma mark - Property accessor(s)
//Late initialisation
-(MCSession *)session{
if(!_session){
_session = [[MCSession alloc]initWithPeer:self.peerId securityIdentity:nil encryptionPreference:MCEncryptionNone];
_session.delegate = self;
}
return _session;
}
#pragma mark - MCSessionDelegate Methods
//Detecting the change in the connection state of the peer
-(void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state{
if (state == MCSessionStateConnecting) {
NSLog(@"Connecting to %@", peerID.displayName);
} else if (state == MCSessionStateConnected) {
NSLog(@"Connected to %@", peerID.displayName);
} else if (state == MCSessionStateNotConnected) {
NSLog(@"Disconnected from %@", peerID.displayName);
}
}
//Handling the data recieved by the session
-(void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID{
[self.delegate session:self didRecieveData:data];
}
//Handling the stream recieved by the session
-(void)session:(MCSession *)session didReceiveStream:(NSInputStream *)stream withName:(NSString *)streamName fromPeer:(MCPeerID *)peerID{
[self.delegate session:self didRecieveAudioStream:stream];
}
//Started recieving stream
-(void)session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progress{
}
//Finished recieving stream
-(void)session:(MCSession *)session didFinishReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID atURL:(NSURL *)localURL withError:(NSError *)error{
}
-(void)session:(MCSession *)session didReceiveCertificate:(NSArray *)certificate fromPeer:(MCPeerID *)peerID certificateHandler:(void (^)(BOOL))certificateHandler{
certificateHandler(YES);
NSLog(@"Recieved a certificate");
}
#pragma mark - MCBrowserViewControllerDelegate methods
//Browser view controller has completd
-(void)browserViewControllerDidFinish:(MCBrowserViewController *)browserViewController{
[browserViewController dismissViewControllerAnimated:YES completion:nil];
}
//Browser view controller is cancelled
-(void)browserViewControllerWasCancelled:(MCBrowserViewController *)browserViewController{
[browserViewController dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - Advertising and Browsing
//Initialise the browser
-(MCBrowserViewController *)browserViewControllerForServiceType:(NSString *)type{
MCBrowserViewController *browser = [[MCBrowserViewController alloc]initWithServiceType:type session:self.session];
browser.delegate = self;
return browser;
}
//Starting the advertiser for the particular service type
-(void)startAdvertisingForServiceType:(NSString *)type discoveryInfo:(NSDictionary *)info{
if(!self.advertiser)
self.advertiser = [[MCAdvertiserAssistant alloc]initWithServiceType:type discoveryInfo:info session:self.session];
[self.advertiser start];
}
//Stoping the advertiser
-(void)stopAdvertising{
[self.advertiser stop];
}
#pragma mark - Misc methods
//Creating the output stream in the current session
-(NSOutputStream *)outputStreamForPeer:(MCPeerID *)peerId{
NSError *error;
NSOutputStream *stream = [self.session startStreamWithName:@"Music" toPeer:peerId error:&error];
if(error)
NSLog(@"%@",[error userInfo].description);
return stream;
}
//Sending the date to all the peers connected to the session
-(void)sendData:(NSData *)data{
NSError * error;
[self.session sendData:data toPeers:self.session.connectedPeers withMode:MCSessionSendDataUnreliable error:&error];
if(error)
NSLog(@"%@",[error userInfo].description);
}
-(NSArray *)connectedPeers{
return [self.session connectedPeers];
}
@end
这是我在 mpc 中的会话文件。我得到的错误是
Project[7347:876615] [GCKSession] 未处于连接状态,因此放弃通道 [0] 上的参与者 [12ECD777]。 和
Project[7347:876615] [ViceroyTrace] [ICE][ERROR] ICEStopConnectivityCheck() 没有找到带有呼叫 ID (317511543) 的 ICE 检查
Project[7347:876751] [ViceroyTrace] [ICE][ERROR] 发送 BINDING_REQUEST 失败(C01A0041)。
需要帮助
【问题讨论】:
-
如果需要更多信息,请告诉我
-
我正在使用 ios 中的一个名为 MPC 的类。所以它也可以在swift中使用。这就是为什么我认为快速的人也可能会有所帮助@EricAya
标签: ios multipeer-connectivity