MultipeerConnectivity会话管理

时间:2013-11-02 14:19:34

标签: ios7 multipeer-connectivity

我现在真的陷入困境,试图掌握MultipeerConnectivityFramework中的邀请。

现在我有一个充当广告商的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”时,即使代理找到了一个,也没有连接的对等体。我在那里犯了错误吗?

1 个答案:

答案 0 :(得分:3)

您的问题是,当您调用invitePeer时,您的会话为空:toSession:withContext:timeout ...无论如何,您有两种方法可以解决此问题。

您有两种选择:
选项1 - 将 peerID创建会话创建会话委派分配移动到其始终执行的位置。例如,在MultipeerConnectivityService类的init代码中,如果它是viewDidLoad中的UIViewController。

选项2 - 在调用“invitePeer:toSession:withContext:timeout:”之前添加以下代码段

if (!_session) {
      MCPeerID *peerID = [[MCPeerID alloc] initWithDisplayName:@"Browser"]; // you can customize the name here
     _session = [[MCSession alloc] initWithPeer:peerID];
     _session.delegate = self;
}

希望这有助于......祝你好运!