2012-01-28 86 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特設網絡功能。該應用程序都在本地網絡上宣傳自己,並自動連接到可用的對等點,建立點對點網絡。