我做了一个蓝牙模式的iPhone游戏,你可以通过蓝牙玩1对1。我对选择器的实现如下:
picker = [[GKPeerPickerController alloc] init];
picker.delegate = self;
picker.connectionTypesMask = GKPeerPickerConnectionTypeNearby;
[picker show];
我不知道哪个代码给出了错误,所以我还将为所有其他与选择器有关的方法粘贴我的代码:
- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context {
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *dataDictionary = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects:dataString, peer, session, nil] forKeys:[NSArray arrayWithObjects:@"data", @"peer", @"session", nil]];
[dataString release];
[self performSelectorOnMainThread:@selector(receivedData:) withObject:dataDictionary waitUntilDone:YES];
}
- (GKSession *)peerPickerController:(GKPeerPickerController *)picker
sessionForConnectionType:(GKPeerPickerConnectionType)type {
// Create a new session if one does not already exist
if (!self.currentSession) {
self.currentSession = [[[GKSession alloc] initWithSessionID:@"Session" displayName:nil sessionMode:GKSessionModePeer] autorelease];
self.currentSession.delegate = self;
}
return self.currentSession;
}
-(void)peerPickerController:(GKPeerPickerController *)pk didConnectPeer:(NSString *)peerID toSession:(GKSession *)session {
self.currentSession = session;
session.delegate = self;
[session setDataReceiveHandler:self withContext:nil];
picker.delegate = nil;
[picker dismiss];
[picker autorelease];
}
-(void)peerPickerControllerDidCancel:(GKPeerPickerController *)pk {
picker.delegate = nil;
[picker autorelease];
[self.navigationController popViewControllerAnimated:YES];
}
// FAIL
- (void)session:(GKSession *)session didFailWithError:(NSError *)error {
NSLog(@"error : %@", [error description]);
}
// SESSION VIND ANDERE SESSION -> CONNECT
-(void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state {
switch (state) {
case GKPeerStateConnected:
NSLog(@"connect met peer %@", [currentSession displayNameForPeer:peerID]);
[self generateRandomNumberAndSendIt];
break;
case GKPeerStateDisconnected:
NSLog(@"disconnected");
[self.currentSession disconnectFromAllPeers];
currentSession = nil;
[self.navigationController popViewControllerAnimated:YES];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Whoopsie" message:@"The connection failed" delegate:nil cancelButtonTitle:@"Okido" otherButtonTitles:nil];
[alert show];
[alert release];
[super viewDidDisappear:YES];
break;
}
}
有时,当我接受传入请求时,选择器会在一台设备上自行删除,而在另一台设备上我会收到错误消息:wait_fences: failed to receive reply: 10004003
. 我认为这与警报视图本身有关。我在此视图中设置了其他警报视图。
我希望你们能帮助我。
提前致谢。