Ios 关闭MCBrowserViewController将断开MCSession

Ios 关闭MCBrowserViewController将断开MCSession,ios,multipeer-connectivity,Ios,Multipeer Connectivity,我已经做了三天多的研究来解决我的问题,但我还没有看到有人能解决我的问题。浏览器邀请广告客户,广告客户接受,MCSession变为已连接状态。但是,一旦通过“取消”或“完成”按钮关闭MCBrowserViewController,MCSession将断开连接。只要我不关闭MCBrowserViewController,MCSession就会保持连接。我不明白这是为什么或者如何工作的,我甚至试过调试这个过程,但它深入到线程中,我无法理解 请告诉我我的代码有问题 -(void)setUpMultiPe

我已经做了三天多的研究来解决我的问题,但我还没有看到有人能解决我的问题。浏览器邀请广告客户,广告客户接受,MCSession变为已连接状态。但是,一旦通过“取消”或“完成”按钮关闭MCBrowserViewController,MCSession将断开连接。只要我不关闭MCBrowserViewController,MCSession就会保持连接。我不明白这是为什么或者如何工作的,我甚至试过调试这个过程,但它深入到线程中,我无法理解

请告诉我我的代码有问题

-(void)setUpMultiPeer{
    self.myPeerID = [[MCPeerID alloc] initWithDisplayName:pos];

    self.mySession = [[MCSession alloc] initWithPeer:self.myPeerID];

    self.browserVC = [[MCBrowserViewController alloc] initWithServiceType:@"svctype" session:self.mySession];

    self.advertiser = [[MCAdvertiserAssistant alloc] initWithServiceType:@"svctype" discoveryInfo:nil session:self.mySession];

    self.browserVC.delegate = self;

    self.mySession.delegate = self;


}

-(void)dismissBrowserVC{
    [self.browserVC dismissViewControllerAnimated:YES completion:nil];
}

-(void)browserViewControllerDidFinish:(MCBrowserViewController *)browserVC{
    [self dismissBrowserVC];
}

-(void)browserViewControllerWasCancelled:(MCBrowserViewController *)browserViewController{
    [self dismissBrowserVC];
}



-(void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state{
    if (state == MCSessionStateConnected) {
        NSLog(@"Connected!");

        //Not entirely sure about this next line...
        self.mySession = session;
    }
    else if (state == MCSessionStateNotConnected){
        NSLog(@"Disconnected");
        dispatch_async(dispatch_get_main_queue(), ^(void) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Somebody Left!"
                                                           message: [[NSString alloc] initWithFormat:@"%@", [peerID displayName]]
                                                          delegate: nil
                                                 cancelButtonTitle:@"Got it"
                                                 otherButtonTitles:nil];
            [alert show];
        });
    }
}

//Called by a UIButton
-(IBAction)browseGO:(id)sender {
    [self setUpMultiPeer];
    [self presentViewController:self.browserVC animated:YES completion:nil];
}

//Called by a UISwitch
-(IBAction)advertiseSwitch:(id)sender {
    if (_advertiseSwitcher.on) {
        [self setUpMultiPeer];
        [self.advertiser start];
    }
    else{
        [self.advertiser stop];
    }
}

我还尝试为每个浏览器和广告客户使用一个独特的MCSession,但没有成功。

我解决问题的方法是从零开始。等待StackOverflow和Apple开发者论坛的答复花了太长时间,所以我回到了最开始的工作,我将从那里再次建立

是我找到的一个很棒的教程的链接。我希望这能帮助别人解决他们的问题

但是,如果有人在问题中看到我的代码有任何错误,请告诉我!我想知道是什么导致了这个错误,这样我就可以从错误中吸取教训


感谢您前来阅读此问题。

面对同样的问题;随时通知我们。