接受邀请多重连接

时间:2013-09-14 22:27:16

标签: ios multipeer-connectivity

我希望我不会通过发布此问题来违反NDA。

我正在使用新的多路连接,使用蓝牙将一些文件发送到附近的设备。我已设法发送邀请,但我似乎没有得到如何显示UIAlertView,用户可以接受或拒绝邀请。现在,当用户发送时,文件会自动保存,并且没有接受/拒绝警​​报。

代码是:

- (void) advertiser:(MCNearbyServiceAdvertiser *)advertiser
didReceiveInvitationFromPeer:(MCPeerID *)peerID
               withContext:(NSData *)context
         invitationHandler:(void(^)(BOOL accept,
                                    MCSession *session))invitationHandler{
... save the data context

但有警告:

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


DevicePeer = [MCPeerID alloc];
DevicePeer = peerID;
ArrayInvitationHandler = [NSArray arrayWithObject:[invitationHandler copy]];

// ask the user
UIAlertView *alertView = [[UIAlertView alloc]
                          initWithTitle:@""
                          message:@""
                          delegate:self
                          cancelButtonTitle:@"NO"
                          otherButtonTitles:@"YES", nil];
[alertView show];
 alertView.tag = 2;
}

和警报视图方法:

 - (void) alertView:(UIAlertView *)alertView
 clickedButtonAtIndex:(NSInteger)buttonIndex
{  
    // retrieve the invitationHandler
    // get user decision
    BOOL accept = (buttonIndex != alertView.cancelButtonIndex) ? YES : NO;
    // respond
    MCSession *session = [ArrayInvitationHandler objectAtIndex:0];

    void (^invitationHandler)(BOOL, MCSession *) = [ArrayInvitationHandler objectAtIndex:0];

    invitationHandler(accept, session);
}

当用户按YES时,应用程序崩溃,我收到错误:

[__NSMallocBlock__ nearbyConnectionDataForPeer:withCompletionHandler:]: unrecognized selector sent to instance 0x14d4e3b0'

我已经查看了IOS开发人员库,除了

之外没有其他方法
- (void)nearbyConnectionDataForPeer:(id)arg1 withCompletionHandler:(id)arg2{

}

哪个没用。没有关于IOS开发人员论坛的信息。有什么想法吗?

2 个答案:

答案 0 :(得分:9)

Alessandro是对的,这在WWDC 2013视频中没有解释。我自己也在努力。

我认为你走在正确的轨道上,你只有几个逻辑错误。 我不明白这两行:

MCSession *session = [ArrayInvitationHandler objectAtIndex:0];
void (^invitationHandler)(BOOL, MCSession *) = [ArrayInvitationHandler objectAtIndex:0];

存储在数组中的对象只是您的处理程序。您遇到崩溃的原因是浏览器看到accept为真,并尝试将对等方连接到会话,但您回送的会话为零。要解决此问题,您需要传回您创建的新会话。

起初我对浏览器创建了一个新会话的概念感到困惑,但后来我意识到我们没有从浏览器那里获得那个会话,我们真的不能如果它不存在,则将其传回邀请函数处理器!

所以是的,请改为:

BOOL accept = (buttonIndex != alertView.cancelButtonIndex) ? YES : NO;

// respond 
MCSession *session;
if(accept) {
   session = [[MCSession alloc] initWithPeer:peer];
   session.delegate = self;
}

void (^invitationHandler)(BOOL, MCSession *) = [ArrayInvitationHandler objectAtIndex:0];
invitationHandler(accept, session);

答案 1 :(得分:1)

我建议您在Apple开发人员中心观看附近网络与Multipeer Connectivity WWDC 2013视频。有一个关于这个东西的例子,这是很好的解释。

PS:是的,你打破了NDA(9月14日),但现在没问题了:)

相关问题