我是第一次使用多点连接框架,并且我想要编程(而不是使用助手类)控制。
当我在两个不同的设备上运行代码时,一切都完全按照描述进行,直到“广告商”收到委托回调为止:
浏览客户端的委托回调在发现广告商时被调用:
-(void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info{
[[[UIAlertView alloc] initWithTitle:@"Peer Found" message:peerID.displayName delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
_session = [[MCSession alloc] initWithPeer:_myPeerID];
_session.delegate = self;
//connect to the discovered peer.
[_browser invitePeer:peerID toSession:_session withContext:nil timeout:30.0];
[_browser stopBrowsingForPeers];
}
然后,广告客户端的委托回调在收到邀请时被调用:
-(void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer:(MCPeerID *)peerID withContext:(NSData *)context invitationHandler:(void (^)(BOOL, MCSession *))invitationHandler{
//when my code runs, everything looks correct here.
//eg. peerID is definitely my 'browser' client's display name etc.
_session = [[MCSession alloc] initWithPeer:_myPeerID];
_session.delegate = self;
//using a simple version for testing... accept all invites.
invitationHandler(YES, _session);
//stop advertising now.
[_advertiser stopAdvertisingPeer];
}
调用“invitationHandler(YES, _session)”后,“浏览”客户端和“广告”客户端之间似乎从未建立连接。
我在任一客户端设备上的 MCSession 对象上从未收到过任何委托回调(我收到过一次或两次 MCSessionStateNotConnected )。我本以为我会收到 MCSession 委托回调:
-(void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state;
我错过了什么吗?还有其他人遇到过这个问题吗?