Search code examples
iosobjective-cgamekitgksession

Not able to connect two devices without using GKPickercontroller


I am trying to connect two devices using Gamekit and transfer small amount of data. I was able to do this with the use GKPickerController but now to make the user experience a little better i am trying to achieve this automatically. The problem is that the delgate method

- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state

is not getting called.

Also these are two different apps.

Please find the code below:

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];

}

Solution

  • Alright..figured this out. For some reason it wasn't working without session id I put the same session id in both the apps and it worked.