接受邀请多重连接 [英] Accepting invitation multipeer connectivity

查看:105
本文介绍了接受邀请多重连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我没有通过发布这个问题来违反NDA。

I hope I am not violating NDA by posting this question.

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

I am using the new multipeer connectivity to send using bluetooth some files to nearby devices. I have managed to send invitations, but I don't seem to get how to display a UIAlertView where the user can accept or decline the invite. Right now when a user sends, the file is automatically saved and there is no accept/decline alert.

代码为:

- (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时,应用程序崩溃,我收到错误:

When the user press YES the app crashes and I get the error:

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

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

I have looked up at the IOS developer library and there is no such method apart from

- (void)nearbyConnectionDataForPeer:(id)arg1 withCompletionHandler:(id)arg2{

}

没有用。没有关于IOS开发人员论坛的信息。任何想法?

which does no work. No info on the IOS developer forums. Any ideas?

推荐答案

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

Alessandro is right, this is not explained in the WWDC 2013 video. I struggled with this myself.

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

I think you're on the right track, you just have a couple logic errors. I don't understand these two lines:

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

存储在数组中的对象只是你的处理程序。您遇到崩溃的原因是浏览器看到 accept 为true并尝试将对等方连接到会话,但是您要回放的会话没有。要解决这个问题,你想要传回你创建的新会话。

The object stored in your array is just your handler. The reason you're getting that crash is that the browser is seeing that accept is true and trying to connect the peer to the session, but the session you're giving it back is nil. To fix this, you want to pass back a new session that you create.

起初我对创建一个新会话的概念感到困惑在浏览器方面,但后来我意识到我们没有从浏览器的任何地方获得该会话,如果不存在,我们无法将其传回邀请处理程序!

At first I was confused by the notion of creating a new session when one has already been created by the browser side, but then I realized that we don't get that session from the browser anywhere, and we can't really pass it back into the invitation handler if it doesn't exist!

所以是的,请改为:

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);

这篇关于接受邀请多重连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆