2012-01-28 78 views
1

为了读取对方的新显示名称,我需要终止并更新GKSession。将它设置为零并重新启动它不起作用。在下面的代码中,的NSLog在for循环以显示可用的同龄人不叫(没有错误消息):GKSession - 终止会话

-(IBAction) btnRefresh:(id) sender { 

    self.currentSession = nil; 

    self.currentSession = [[GKSession alloc] initWithSessionID:@"anything" displayName:name sessionMode:GKSessionModePeer]; 
    self.currentSession.delegate = self; 
    self.currentSession.available = YES; 
    self.currentSession.disconnectTimeout = 0; 
    [self.currentSession setDataReceiveHandler:self withContext:nil]; 

    peerListAvailable = [[NSMutableArray alloc] initWithArray:[currentSession peersWithConnectionState:GKPeerStateAvailable]]; 

    for (NSString *peer in peerListAvailable) { 
     NSLog(@"found available peer; checking name and ID... %@, %@",[currentSession displayNameForPeer:peer], peer); 
    } 

什么是错的设置currentSession到零,重新开始呢? 也许你知道另一种更新GKSession的方法吗? 非常感谢。

回答

3

下面的方法说明GKSession的安装和拆卸:

- (void)setupSession 
{ 
    gkSession = [[GKSession alloc] initWithSessionID:nil displayName:nil sessionMode:GKSessionModePeer]; 
    gkSession.delegate = self; 
    gkSession.disconnectTimeout = 5; 
    gkSession.available = YES; 
} 

- (void)teardownSession 
{ 
    gkSession.available = NO; 
    [gkSession disconnectFromAllPeers]; 
} 

如果你有兴趣钻研更深,看看GKSessionP2P,演示应用程序,说明了GKSession特设网络功能。该应用程序都在本地网络上宣传自己,并自动连接到可用的对等点,建立点对点网络。