Ios MCSession of en can';当应用程序从后台返回前台时,无法重新连接

Ios MCSession of en can';当应用程序从后台返回前台时,无法重新连接,ios,cocoa-touch,ios7,multipeer-connectivity,Ios,Cocoa Touch,Ios7,Multipeer Connectivity,我正在使用多点连接制作聊天应用程序。起初,一切正常,MCSession可以相互连接。但当用户点击主页按钮,然后点击应用程序图标时,应用程序彼此断开连接,通常无法重新连接。但有时它可以重新连接。成功是随机的 - (void)viewDidLoad { [super viewDidLoad]; ... [[NSNotificationCenter defaultCenter] addObserver:self

我正在使用多点连接制作聊天应用程序。起初,一切正常,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中,您会断开与会话的连接,并破坏会话、浏览器和广告客户。在DidBecomeActive中,您有一个防护装置(不是FirstLaunch),这意味着会话和广告客户只在您第一次激活时创建,而不是在您从后台回来后创建。

您找到解决方案了吗?