我已经做了三天多的研究来解决我的问题,但我还没有看到任何人能解决我的问题。浏览器邀请广告商,广告商接受,MCSession变为已连接状态。但是,一旦MCBrowserViewController关闭(通过cancel或done按钮),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,但没有成功。
发布于 2014-01-27 02:58:33
我所做的是从头开始解决我的问题。等待StackOverflow和苹果开发者论坛的回复花费了太长的时间,所以我回到了一开始起作用的地方,我将从那里再次建立起来。
Here是我找到的一个很棒的教程的链接。我希望这能帮助别人解决他们的问题。
但是,如果有人在问题中看到我的代码有任何完全错误的地方,请一定要告诉我!我想知道是什么导致了这个bug,这样我就可以从我的错误中吸取教训。
感谢您的光临并阅读了这个问题。
https://stackoverflow.com/questions/21209528
复制相似问题