這是我第一次發佈在stackoverflow上,我知道嚴格的發佈要求。如果我沒有遵循任何指導方針,請告訴我。Inviting Peer to MCSEssion
我目前正在使用Objective-C在Xcode中編寫IOS(8.4)應用程序。目標是使用MCSession來在IOS設備之間傳輸數據。儘管在這裏和其他地方閱讀了大量的帖子,但我仍然在努力理解這個話題。下面是我的資源已經知道:
https://developer.apple.com/videos/play/wwdc2013-708/
https://medium.com/@phoenixy/using-multipeer-connectivity-120abacb9db
這是我目前的理解:在最基本的層面上,有廣告商和瀏覽器。廣告客戶有一個本地會話,可以讓他們「做廣告」。當瀏覽器看到廣告客戶時,瀏覽器向廣告客戶發送邀請給他(瀏覽器)的本地MCSession。假設這是完全正確的,這裏是我感到困惑的地方。廣告客戶可以接受邀請,並在此過程中將其本地會話傳遞給inviteHandler。
我已經在代碼中實現了以下邏輯,如下所示。但是,在追蹤廣告客戶和瀏覽器的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);
}
}
任何幫助,不勝感激!
奧斯汀