gpt4 book ai didi

ios - 应用程序从后台返回前台时,MCSession 经常无法重新连接

转载 作者:行者123 更新时间:2023-11-29 03:04:55 24 4
gpt4 key购买 nike

我正在使用 Mutipeer Connectivity 制作聊天应用程序。一开始,一切正常,MCSession 可以相互连接。但是,当用户点击主页按钮,然后点击应用程序图标时,应用程序之间会断开连接,并且经常无法重新连接。但有时它可以重新连接。成功是随机的。

    - (void)viewDidLoad
{
[super viewDidLoad];
...

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(DidBecomeActive:)
name: UIApplicationDidBecomeActiveNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didEnterBackground:)
name: UIApplicationDidEnterBackgroundNotification object:nil];

NSString *displayname=[[NSUUID UUID] UUIDString];
_MyPeerID=[[MCPeerID alloc] initWithDisplayName:displayname];

}

- (void)DidBecomeActive:(NSNotification *)notification
{

if (notFirstLaunch==YES) {
NSLog(@"DidBecomeActive");
NSString *displayname=[[NSUUID UUID] UUIDString];
_MyPeerID=[[MCPeerID alloc] initWithDisplayName:displayname];
[self createSession];
[self createAdvertiser];
[self beginBrowsing];


}
notFirstLaunch=YES;

}

- (void)didEnterBackground:(NSNotification *)notification
{
NSLog(@"didEnterBackground");
[_MySession disconnect];
_MySession=nil;
_MyBrowser=nil;
_MyAdver=nil;


}



- (void)createSession
{
_MySession=[[MCSession alloc] initWithPeer:_MyPeerID];
_MySession.delegate=self;
}


- (void)createAdvertiser
{
_MyAdver=[[MCNearbyServiceAdvertiser alloc] initWithPeer:_MyPeerID discoveryInfo:nil serviceType:MyServiceType];
_MyAdver.delegate=self;
[_MyAdver startAdvertisingPeer];
}

- (void)beginBrowsing {
_MyBrowser=[[MCNearbyServiceBrowser alloc] initWithPeer:_MyPeerID serviceType:MyServiceType];
_MyBrowser.delegate=self;
[_MyBrowser startBrowsingForPeers];
}

最佳答案

从您发布的代码来看,没有任何重新连接,因为在 didEnterBackground 中您断开了与 session 的连接并销毁了 session 、浏览器和广告商。在 DidBecomeActive 中,您有一个守卫 (notFirstLaunch),这意味着 session 和广告商仅在您第一次激活时创建,而不是在您从后台返回时激活后创建。

关于ios - 应用程序从后台返回前台时,MCSession 经常无法重新连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22891335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com