Gamekit を使用して 2 つのデバイスを接続し、少量のデータを転送しようとしています。GKPickerController を使用してこれを行うことができましたが、ユーザー エクスペリエンスを少し向上させるために、これを自動的に達成しようとしています。問題はデリゲートメソッド
- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state
が呼び出されていません。
また、これらは 2 つの異なるアプリです。
以下のコードを見つけてください。
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];
}