1

In order to read a new display name of a peer I need to kill and renew the GKSession. Setting it to nil and initiate it anew does not work. In the code below, the NSLog in the for-loop to show the available peers is not called (there's no error message):

-(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);
    }

What is wrong with setting the currentSession to nil and initiate it anew? Maybe you know of another way to renew a GKSession? Thanks very much in advance.

4

1 回答 1

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. 该应用程序既在本地网络上宣传自己,又自动连接到可用的对等点,从而建立一个点对点网络。

于 2012-01-30T08:52:54.167 回答