Multipeer连接存在问题,接受来自其他对等方的传入邀请。我已经将邀请函块的代码缩小了。请求从一个对等体发送到广告商,请求在didReceiveInvitationFromPeer:delegate方法中看到,但用于响应该请求的块似乎失败,无论是否从未发送响应并且didChangeState:delegate方法超时发送请求的同行。
代码如下:
从同行发送的邀请请求
[[[_appDelegate mcManager]browser] invitePeer:_chosenBroadcast toSession:[[_appDelegate mcManager]session] withContext:initialData timeout:0];
(您可以将超时设为0,因为默认超时将被应用,即30秒)
广告客户didReceiveInvitationFromPeer:委托方法
-(void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer:(MCPeerID *)peerID withContext:(NSData *)context invitationHandler:(void (^)(BOOL, MCSession *))invitationHandler{
AppDelegate *appDelegate;
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.ArrayInvitationHandler = [NSArray arrayWithObject:[invitationHandler copy]];
NSLog(@"Received Request");
NSString *contextData = [NSString stringWithUTF8String:[context bytes]];
if ([contextData isEqual:[NSString stringWithFormat:@"InitialRequest"]]) {
// Additional operations
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"MCNewPeer"
object:nil
userInfo:dict];
}
-(void)stateChangedWithNotification:(NSNotification *)notification {
void (^invitationHandler)(BOOL, MCSession *) = [_appDelegate.ArrayInvitationHandler objectAtIndex:0];
invitationHandler(YES, _appDelegate.mcManager.session);
NSLog(@"session connected peers %lu", (unsigned long)[[[[_appDelegate mcManager]session]connectedPeers]count]);
}
代表已在类中设置如下:
-(void)setupPeerAndSessionWithDisplayName:(NSString *)displayName{
_peerID = [[MCPeerID alloc] initWithDisplayName:displayName];
_session = [[MCSession alloc] initWithPeer:_peerID];
_session.delegate = self;
}
-(void)setupMCBrowser{
_browser = [[MCNearbyServiceBrowser alloc] initWithPeer:_peerID serviceType:@"com-audio-send"];
_browser.delegate = self;
}
(我使用调试来检查是否已将正确的变量和标识传递给各种方法。)
我在这里缺少什么?