简体   繁体   English

接受邀请多重连接

[英]Accepting invitation multipeer connectivity

I hope I am not violating NDA by posting this question. 我希望我没有通过发布这个问题来违反NDA。

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. 我已设法发送邀请,但我似乎没有得到如何显示UIAlertView,用户可以接受或拒绝邀请。 Right now when a user sends, the file is automatically saved and there is no accept/decline alert. 现在,当用户发送时,文件会自动保存,并且没有接受/拒绝警​​报。

The code is: 代码是:

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

but with the alert: 但有警报:

- (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;
}

and the alert view method: 和警报视图方法:

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

When the user press YES the app crashes and I get the error: 当用户按YES时,应用程序崩溃,我收到错误:

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

I have looked up at the IOS developer library and there is no such method apart from 我已经查看了IOS开发人员库,除此之外没有其他方法

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

}

which does no work. 这没用。 No info on the IOS developer forums. 没有关于IOS开发人员论坛的信息。 Any ideas? 有任何想法吗?

Alessandro is right, this is not explained in the WWDC 2013 video. 亚历山德罗是对的,这在WWDC 2013视频中没有解释。 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];

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. 你遇到崩溃的原因是浏览器看到accept是真的并且试图将对等体连接到会话,但是你给它的会话是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! 起初我对浏览器端已经创建了一个新会话的概念感到困惑,但后来我意识到我们没有从浏览器那里得到那个会话,我们无法真正传回去进入邀请处理程序,如果它不存在!

So yeah, do this instead: 所以,是的,改为:

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

I suggest you to watch Nearby Networking with Multipeer Connectivity WWDC 2013 video on the Apple developers center. 我建议您在Apple开发人员中心观看附近网络与Multipeer Connectivity WWDC 2013视频。 There is an example about this stuff and this is very well explained. 有一个关于这个东西的例子,这是很好的解释。

PS : Yes you break NDA (september 14) but now it's ok :) PS:是的你打破了NDA(9月14日),但现在可以了:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM