关闭MCBrowserViewController断开MCSession
问题描述:
我已经做了三天多的研究来解决我的问题,我还没有看到任何人解决我的问题。浏览器邀请广告商,广告商接受,并且MCSession更改为连接状态。但是,一旦MCBrowserViewController关闭(通过取消或完成按钮),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和苹果开发者论坛的答案耗时太长,所以我回到了刚开始工作的时候,我将从此再次建立起来。
Here是我找到的一个令人敬畏的教程的链接。我希望这可以帮助别人解决他们的问题。
但是,如果有人看到我的代码在问题中完全错误请不要说!我想知道是什么导致了这个错误,以便我可以从我的错误中吸取教训。
谢谢你停下来阅读这个问题。
面对同样的事情;保持我们的发布。 – davecom