createSession in GKGameSession is only returning nil

470 views Asked by At

I'm trying to start a new GKGameSession and when I use createSession, all I can get so far is nil. Here's my code:

GKGameSession.createSession(inContainer: "test", withTitle: "MyGame", maxConnectedPlayers: 8)
    { (newGameSession, error) in
        self.gameSession = newGameSession
        print("\(newGameSession)")
        newGameSession?.getShareURL(completionHandler: { (url, error) in
            print("url: \(url) error: \(error)")
        })
    }

The only thing it prints is "nil". Any help is appreciated.

4

There are 4 answers

2
Kevin Packard On

If you using the emulator, I suggest using a device instead. GKGameSessions do not play well with the emulator, because they depend on push notifications and an iCloud account that is logged in.

2
James Zaghini On

newGameSession is optional. So it seems like something has gone wrong when creating a new session.

I would say newGameSession is likely nil, in that case error will hopefully contain some useful information.

Try replacing print("\(newGameSession)") with print(newGameSession, error) to see what the error var has to say, or set a breakpoint if you know how to do that.

3
OzSimTech On

Try to use your app's iCloud container name instead of "test". The container name will be in the format iCloud.com.yourcompany.appname if you have selected the default container option. To ensure your app has an iCloud container you need to enable it in your app's Capabilities.

1
Thunk On

I'm a jumping in a little late, but I'd triple check the container name you're using to create the session.

I enabled all three options in xCode: key-value storage, iCloud documents and CloudKit.

I don't use iCloud drive.

The following code successfully creates a new session each time I send invites. It prints the new session, then iterates through all existing sessions for that container ID.

-(void)turnBasedMatchmakerViewController:(GKTurnBasedMatchmakerViewController *)viewController didFindMatch:(GKTurnBasedMatch *)match
{
    [self dismissViewControllerAnimated:YES completion:nil];

    NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];    
    NSString *iCloudContainerName = [@"iCloud." stringByAppendingString: bundleIdentifier];


    [GKGameSession createSessionInContainer:iCloudContainerName
                                           withTitle:@"test"
                                 maxConnectedPlayers:4
                                   completionHandler:^(GKGameSession * _Nullable session, NSError * _Nullable error)
     {
         NSLog(@"(1) Session: %@, Error: %@", session.identifier, [error description]);

         [GKGameSession loadSessionsInContainer:iCloudContainerName
                          completionHandler:^(NSArray<GKGameSession *> * _Nullable sessions, NSError * _Nullable error)
          {
              for (GKGameSession *session in sessions)
              {
                  NSLog(@"(2) Session: %@, Error: %@", session.identifier, [error description]);
              }

              NSLog(@"-----");
          }];

     }];
}