这是我第一次在 stackoverflow 上发帖,我知道严格的发帖要求。如果我没有遵循任何准则,请告诉我。
我目前正在使用 Objective-C 在 Xcode 中编写一个 IOS (8.4) 应用程序。目标是使用 MCSessions 在 IOS 设备之间传输数据。尽管在此处和其他地方阅读了许多试图阐明该主题的帖子,但我目前仍在为会话的概念而苦苦挣扎。以下是我已经知道的资源:
https://developer.apple.com/videos/play/wwdc2013-708/
https://medium.com/@phoenixy/using-multipeer-connectivity-120abacb9db
这是我目前的理解:在最基本的层面上,你有一个广告商和一个浏览器。广告商有一个本地会话,允许他们“做广告”。当浏览器看到广告商时,浏览器会向广告商发送到他(浏览器的)本地 MCSession 的邀请。假设这一切都是正确的,这就是我感到困惑的地方。广告商可以接受邀请,并在此过程中将他的本地会话传递给invitationHandler。
我在代码中实现了如下逻辑,如下图。但是,在跟踪广告商和浏览器的 MCSession 状态更改时,会尝试连接,但最终状态始终是 didNotNonnect。
发送邀请码(浏览器):
[self.broadcasterBrowser invitePeer:[broadcasterPeerIDs objectAtIndex:indexPath.row]
toSession: self.appDelegate.mpcHandler.session withContext:nil timeout:30.0 ];
接受邀请代码(广告商):
- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser
didReceiveInvitationFromPeer:(MCPeerID *)peerID withContext:(NSData *)context invitationHandler:(void(^)(BOOL accept, MCSession *session))invitationHandler
{
ArrayInvitationHandler = [NSArray arrayWithObject:[invitationHandler copy]];
// ask the user
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:peerID.displayName
message:@"Would like to create a session with you"
delegate:self
cancelButtonTitle:@"Decline"
otherButtonTitles:@"Accept", nil];
[alertView show];
if (alertViewResult)
{
void (^invitationHandler)(BOOL, MCSession *) = [ArrayInvitationHandler objectAtIndex:0];
invitationHandler(YES, self.appDelegate.mpcHandler.session);
}
}
任何帮助是极大的赞赏!
奥斯汀