为了读取对等点的新显示名称,我需要杀死和更新GKSession。将其设置为零并重新启动它是行不通的。在下面的代码中,for-循环中显示可用对等点的NSLog不被调用(没有错误消息):
-(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设置为0并重新启动它有什么问题?也许你知道另一种更新GKSession的方法?提前谢谢。
发布于 2012-01-30 08:52:54
下面的方法说明了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的临时网络功能。该应用程序既在本地网络上做广告,又自动连接到可用的对等网络,从而建立了对等网络。
https://stackoverflow.com/questions/9048441
复制相似问题