Reputation: 359
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.
Upvotes: 1
Views: 1235
Reputation: 6692
The following methods illustrate GKSession
setup and teardown:
- (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];
}
If you're interested in delving deeper, take a look at GKSessionP2P, a demo app that illustrates the ad-hoc networking features of GKSession
. The app both advertises itself on the local network and automatically connects to available peers, establishing a peer-to-peer network.
Upvotes: 3