Multipeer连接:接受邀请(使用内置浏览器VC) [英] Multipeer Connectivity: getting an invitation accepted (using built-in browser VC)

查看:266
本文介绍了Multipeer连接:接受邀请(使用内置浏览器VC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按照WWDC讨论来了解MultipeerConnectivity框架。

I'm trying to follow the WWDC talk to learn about the MultipeerConnectivity framework. After many false starts, the browser(s) show the peers, and invitations get issued.

但是当我在对等设备上按接受时,浏览器会继续显示连接无端。我认为 MCBrowserViewController 负责处理逻辑,我可以放松,直到浏览器的用户按下取消或完成,并触发委托方法。我敢打赌,这是明显的东西,但它是躲我。

But when I press "Accept" on the peer device, the browser keeps showing "Connecting" without end. I thought that the MCBrowserViewController took care of the logic and I could relax until the browser's user pressed Cancel or Done, and the delegate method fired. I bet it's something obvious, but it's eluding me.

这里是我希望的是相关的代码。我有它在AppDelegate。当然,除了 browserViewControllerDidFinish:中的一个,各种委托方法中的NSLog语句都会被调用。

Here's what I hope is the relevant code. I have it in the AppDelegate. NSLog statements in the various delegate methods get called as I would expect—except for the one in browserViewControllerDidFinish: of course.

请记住,浏览器和邀请确实出现,所以是正确的!

Bear in mind that the browser and invitations do appear, so something is right!

在@interface ...

In the @interface...

@property   (strong, nonatomic) MCSession   *theSession;
@property   (strong, nonatomic) MCAdvertiserAssistant       *assistant;
@property   (strong, nonatomic) MCBrowserViewController     *browserVC;

在@implementation

In the @implementation

static    NSString* const    kServiceType = @"eeps-multi";

// called from viewDidAppear in the main ViewController

-(void)     startSession
{
    if (!self.theSession) {
        UIDevice *thisDevice = [UIDevice currentDevice];

        MCPeerID *aPeerID = [[ MCPeerID alloc ] initWithDisplayName: thisDevice.name];
        self.theSession = [[ MCSession alloc ] initWithPeer: aPeerID ];
        self.theSession.delegate = self;
    } else {
        NSLog(@"Session init skipped -- already exists");
    }
}

// called from viewDidAppear in the main ViewController

- (void)    startAdvertising
    {
    if (!self.assistant) {
        self.assistant = [[MCAdvertiserAssistant alloc] initWithServiceType:kServiceType
                                                              discoveryInfo:nil
                                                                    session:self.theSession ];
        self.assistant.delegate = self;
        [ self.assistant start ];
    } else {
        NSLog(@"Advertiser init skipped -- already exists");
    }
}

// called from the main ViewController in response to a button press

- (void)    startBrowsing
{
    if (!self.browserVC){
        self.browserVC = [[MCBrowserViewController alloc] initWithServiceType:kServiceType 
                                                                      session:self.theSession];
        self.browserVC.delegate = self;
    } else {
        NSLog(@"Browser VC init skipped -- already exists");
    }

    [ self.window.rootViewController presentViewController:self.browserVC animated:YES completion:nil];
}

提前感谢!

推荐答案

感谢评论者的优秀建议,导致我发现我自己的错误。这里是:

Thanks to the commenters for excellent suggestions that led to my finding my own mistake. And here it is:

如果实现 MCSessionDelegate 方法 session:didReceiveCertificate:fromPeer :certificateHandler 方法,它将拦截对等体尝试连接到会话。

If you implement the MCSessionDelegate method session:didReceiveCertificate:fromPeer:certificateHandler method, it will intercept the peer's attempt to connect to the session. You should either explicitly approve that connection in that method or comment it out.

详细信息和经验教训:

除了我展示的代码,我已经做了各种委托方法的短暂实现。一个 MCSessionDelegate 方法是这样的:

In addition to the code I showed, I had made stubby implementations of the various delegate methods. One MCSessionDelegate method is this one:

- (void)          session:(MCSession *)session 
    didReceiveCertificate:(NSArray *)certificate 
                 fromPeer:(MCPeerID *)peerID 
       certificateHandler:(void (^)(BOOL))certificateHandler
{

}

扩展@ Big-O Claire的建议,所有这些委托方法,Just In Case。当对等体点击AdvertiserAssistant UI中的接受按钮时,此对话框触发。

Extending @Big-O Claire 's advice above, I started watching all these delegate methods, Just In Case. And this one fired when the peer tapped the Accept button in the AdvertiserAssistant UI.

此方法允许您决定对等设备是否合法, certificateHandler:)如果你不想。 Apple说,

This method gives you a chance to decide if the peer is legit and not connect (using the certificateHandler:) if you don't want to. Apple says,


您的应用程序应检查附近的对等体证书,然后决定是否信任该证书。确定后,您的应用程序应调用提供的certificateHandler块,传递YES(以信任附近的对等体)或NO(拒绝它)。

Your app should inspect the nearby peer’s certificate, and then should decide whether to trust that certificate. Upon making that determination, your app should call the provided certificateHandler block, passing either YES (to trust the nearby peer) or NO (to reject it).

此外,您还可以获得此提示:

In addition, you get this tip:


重要:多管道连接框架尝试以任何方式验证对等体提供的身份或证书。

Important: The multipeer connectivity framework makes no attempt to validate the peer-provided identity or certificates in any way. If your delegate does not implement this method, all certificates are accepted automatically.

当我注释掉这个方法时,连接就会完成 - 如果你的代理没有实现这个方法,和这个问题,至少,解决了。

When I commented this method out, the connections went through — and THIS problem, at least, was solved.

这篇关于Multipeer连接:接受邀请(使用内置浏览器VC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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