我正在尝试使用Gamekit连接两台设备并传输少量数据。我能够通过使用GKPickerController来做到这一点,但现在为了让用户体验更好一些,我试图自动实现这一点。问题是delgate方法
- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state
没有被调用。
这些是两个不同的应用程序。
请找到以下代码:
iPad app
AppDelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate,GKSessionDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, retain) GKSession *connectSession;
@end
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
_peerDevice = [[NSMutableArray alloc]init];
_connectSession = [[GKSession alloc]initWithSessionID:nil displayName:nil sessionMode:GKSessionModePeer];
_connectSession.delegate = self;
_connectSession.available = YES;
_connectSession.disconnectTimeout = 0;
[_connectSession setDataReceiveHandler:self withContext:nil];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[xyz alloc] initWithNibName:@"xyz" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state{
if(state == GKPeerStateConnected){
// Add the peer to the Array
[_peerDevice addObject:peerID];
NSString *str = [NSString stringWithFormat:@"Connected with %@",[session displayNameForPeer:peerID]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connected" message:str delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
// Used to acknowledge that we will be sending data
[session setDataReceiveHandler:self withContext:nil];
[[self.window viewWithTag:12] removeFromSuperview];
}
}
- (void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID
{
NSLog(@"didReceiveConnectionRequestFromPeer: %@", [session displayNameForPeer:peerID]);
[session acceptConnectionFromPeer:peerID error:nil];
}
iPhone app
AppDelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate, GKSessionDelegate,UIAlertViewDelegate>
@property (nonatomic, strong) GKSession *connectSession;
@property (strong, nonatomic) UIWindow *window;
@end
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
_peerDevice = [[NSMutableArray alloc]init];
_connectSession = [[GKSession alloc]initWithSessionID:nil displayName:nil sessionMode:GKSessionModePeer];
_connectSession.delegate = self;
_connectSession.available = YES;
_connectSession.disconnectTimeout = 0;
[_connectSession setDataReceiveHandler:self withContext:nil];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
_nav = [[UINavigationController alloc]initWithNavigationBarClass:[CustomNavigationBar class] toolbarClass:nil];
self.viewController = [[HCAViewController alloc] initWithNibName:@"HCAViewController" bundle:nil];
[_nav pushViewController:self.viewController animated:YES];
self.window.rootViewController = _nav;
[self.window makeKeyAndVisible];
return YES;
}
- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state{
_connectSession = session;
NSString * str;
UIAlertView * alert;
switch (state) {
case GKPeerStateAvailable:
[session connectToPeer:peerID withTimeout:0];
break;
case GKPeerStateConnected:
[_peerDevice addObject:peerID];
str = [NSString stringWithFormat:@"Connected with SFMedical"];
alert = [[UIAlertView alloc] initWithTitle:@"Connected" message:str delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
// Used to acknowledge that we will be sending data
[session setDataReceiveHandler:self withContext:nil];
[[self.window viewWithTag:12] removeFromSuperview];
break;
default:
break;
}
}
- (void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID
{
NSLog(@"didReceiveConnectionRequestFromPeer: %@", [session displayNameForPeer:peerID]);
[session acceptConnectionFromPeer:peerID error:nil];
}
答案 0 :(得分:0)
好吧......把它搞定了。出于某种原因,如果没有会话ID,它就无法工作。我在两个应用程序中都添加了相同的会话ID,并且它有效。