如何禁用连接警报(ios multipeer连接框架)

时间:2014-08-13 14:04:19

标签: ios ios7 multipeer-connectivity

我发现了这一点: [Disable connection alert (Multipeer connectivity)

其中的代码是:

 - (void)advertiser:(MCNearbyServiceAdvertiser *)theAdvertiser didReceiveInvitationFromPeer:(MCPeerID *)directorPeerId withContext:(NSData *)invitationData invitationHandler:(void (^)(BOOL, MCSession *))invitationHandler
{
    // accept the invitation and stop advertising
    invitationHandler(YES, self.session);
    [self.advertiser stopAdvertisingPeer];
    self.advertiser = nil;
}

但是我不知道如何使这段代码工作,它应该在哪里实现以及如何触发它?

2 个答案:

答案 0 :(得分:0)

以下是我所做的一些片段:

在一台设备上 - 启动浏览器

_serviceBrowser = [[MCNearbyServiceBrowser alloc] initWithPeer:_peerID
                                                   serviceType:_sessionName];

[_serviceBrowser startBrowsingForPeers];

在另一台设备上 - 启动广告客户

_serviceAdvertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:_peerID
                                                           discoveryInfo:nil
                                                             serviceType:_sessionName];
[_serviceAdvertiser startAdvertisingPeer];

这些服务实现委托功能,以便为您的应用提供可能的连接建议。在这里,浏览器被告知广告商,现在邀请对等方加入会话

- (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID     withDiscoveryInfo:(NSDictionary *)info
{
    [browser invitePeer:peerID toSession:_session withContext:nil timeout:30.0];

}

广告客户随后回复

- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer:    (MCPeerID *)peerID withContext:(NSData *)context invitationHandler:(void(^)(BOOL accept, MCSession *session))invitationHandler
{
    invitationHandler(YES, _session);
}

现在,您将收到一个会话委托调用,以告知您对等连接的状态。此时你应该连接 - 不需要“真正的”用户交互。

- (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state

答案 1 :(得分:-1)

在实现MCNearbyServiceAdvertiserDelegate协议的委托上调用该方法。

要收到加入会话的邀请,您需要宣传您愿意这样做。例如:

MCPeerID *peerID = [[MCPeerID alloc] initWithDisplayName:@"Peer Name"];

// Here we set a nearby service advertiser that tells nearby peers that this app is willing to join sessions of a specified type.
self.advertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:peerID discoveryInfo:nil serviceType:@"myservice"];
self.advertiser.delegate = self;
[self.advertiser startAdvertisingPeer];

当附近的浏览对手听到你时,它可能会发出加入会话的邀请,当它这样做时,你提到的方法将在代表上调用,你可以接受你引用的代码。