我真的被困在現在試圖在MultipeerConnectivityFramework中掌握邀請。MultipeerConnectivity會話管理
現在我有一個ipad應用充當廣告客戶和一個iphone應用充當瀏覽器。 我已經實現了MultipeerFramework一個sharedService,也做了以下內容:
廣告
@implementation MultipeerConnectivityService {
MCNearbyServiceAdvertiser *_advertiser;
MCSession *_session;
MCNearbyServiceBrowser *_browser;
}
- (void)automaticAdvertiseWithName:(NSString *)name {
MCPeerID *peerID = [[MCPeerID alloc] initWithDisplayName:name];
_session = [[MCSession alloc] initWithPeer:peerID];
_session.delegate = self;
_advertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:peerID discoveryInfo:nil serviceType:kServiceType];
_advertiser.delegate = self;
[_advertiser startAdvertisingPeer];
}
- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer:(MCPeerID *)peerID withContext:(NSData *)context invitationHandler:(void (^)(BOOL, MCSession *))invitationHandler {
invitationHandler([@YES boolValue], _session);
NSLog(@"Invitation accepted");
}
瀏覽器
- (void)automaticBrowsingWithName:(NSString *)name {
MCPeerID *peerID = [[MCPeerID alloc] initWithDisplayName:name];
_browser = [[MCNearbyServiceBrowser alloc] initWithPeer:peerID serviceType:kServiceType];
_browser.delegate = self;
[_browser startBrowsingForPeers];
}
- (void)browser:(MCNearbyServiceBrowser *)browser didNotStartBrowsingForPeers:(NSError *)error {
if ([_delegate respondsToSelector:@selector(browser:didNotStartBrowsingForPeers:)]) {
[_delegate browserDidNotStartBrowsingForPeers];
}
}
- (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info {
[browser invitePeer:peerID toSession:[self getMCSession] withContext:nil timeout:10];
if ([_delegate respondsToSelector:@selector(browser:foundPeer:)]) {
[_delegate browser:browser foundPeer:peerID];
}
}
- (void)browser:(MCNearbyServiceBrowser *)browser lostPeer:(MCPeerID *)peerID {
if ([_delegate respondsToSelector:@selector(browserLostPeer:)]) {
[_delegate browserLostPeer:peerID];
}
}
- (MCSession *) getMCSession {
return _session;
}
但後來我越來越爲控制檯反饋:
-[MCNearbyServiceBrowser invitePeer:toSession:withContext:timeout:] Bad argument session=nil
當我查找找到的廣告客戶時,一切正常。我的廣告客戶iPad正在被找到。但我如何管理邀請?
所以我現在不明白...當我通過瀏覽器發送邀請時,我必須使用什麼會話?在iPad上,我可以像在「automaticAdvertiseWithName」方法中看到的那樣設置會話。但在iPhone上,我不這樣做時,調用「automaticBrowsingWithName」...是這樣的問題?他們不一定要成爲同一個會話才能成功連接它們嗎?我怎樣才能成功邀請我的廣告客戶iPad到瀏覽器?
當廣告客戶已經創建新會話時,我感到困惑。
我實際上並不確定,如果代理didReceiveInvitation將對等體添加到connectedPeers中。
- (void)automaticAdvertiseWithName:(NSString *)name {
MCPeerID *peerID = [[MCPeerID alloc] initWithDisplayName:name];
self.session = [[MCSession alloc] initWithPeer:peerID];
self.session.delegate = self;
_advertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:peerID discoveryInfo:nil serviceType:kServiceType];
_advertiser.delegate = self;
[_advertiser startAdvertisingPeer];
}
- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer:(MCPeerID *)peerID withContext:(NSData *)context invitationHandler:(void (^)(BOOL, MCSession *))invitationHandler {
BOOL accept = YES;
invitationHandler(accept, self.session);
NSLog(@"Invitation accepted: %@", self.session);
}
當我在會話中調用屬性「connectedPeers」時,即使委託人找到一個屬性,根本沒有連接的對等體。我在那裏犯錯了嗎?
謝謝!我去了選項2,它工作得很好!會議的概念起初有點難以籠絡我的大腦。我一直認爲我將不得不使用同一個會話.... – sesc360
很高興的選項2幫助! – Beuz
我也對此感到困惑。我知道一個瀏覽器是爲了將會話添加到會話中,但我仍然不確定廣告客戶要做什麼。目前,我只是假設廣告客戶在「inviteHandler」中提供會話時,會以某種方式與瀏覽器將其添加到的會話「合併」。 @Beuz或@ISARFAKTOR,如果你有這方面的見解,請分享。 – Yazid