1

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.

Thunk
  • 4,099
  • 7
  • 28
  • 47
Bryan Cimo
  • 1,268
  • 1
  • 19
  • 34
  • As soon as iOS 10 was out of Beta, I was able to get this to work. – Bryan Cimo Nov 17 '16 at 17:44
  • 1
    Where are you guys finding documentation on any of this? Apple documentation only has the method signatures. Do any guides exist online on how to use this API? – Shaun Budhram Dec 21 '16 at 09:14
  • There are no docs, but the classes & methods are fairly simple. Start with the WWDC 2016 GameCenter video, session 611 - https://developer.apple.com/videos/play/wwdc2016/611/ – Kevin Packard Apr 06 '17 at 03:45

4 Answers4

3

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.

Kevin Packard
  • 374
  • 4
  • 13
  • 1
    This does not provide an answer to the question. Once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/14271201) – Dave Cousineau Nov 13 '16 at 01:21
  • 2
    Yah well ... before posting an answer, I tried to post my question as a comment, but I do not have not enough points. If you chose to suppress my answer, fine, but do know that GKGameSession beginners are regularly bitten by lack of emulator support. – Kevin Packard Nov 13 '16 at 17:30
2

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.

James Zaghini
  • 3,895
  • 4
  • 45
  • 61
  • took me a while to get back to this, but here's the error: session: nil error: Optional(Error Domain=GKGameSessionErrorDomain Code=2 "The requested operation could not be completed because you are not signed in to iCloud." UserInfo={NSLocalizedDescription=The requested operation could not be completed because you are not signed in to iCloud.}) I'm logged in to iCloud and like OzSimTech's answer suggested, I'm using the container name that matches the app id. Also, I'm logged into game center. – Bryan Cimo Jul 27 '16 at 04:32
  • 1
    Go to settings and make sure you are signed into iCloud and game center and maybe turn things on/off then try again – WolfLink Feb 01 '17 at 18:33
1

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.

OzSimTech
  • 41
  • 3
  • I tried adding the bundle id, like you said, but it's still not working, see the comment I made to the answer by James. – Bryan Cimo Jul 27 '16 at 04:36
  • Some have said that not having iCloud Drive enabled on the device has caused this error although I tried to reproduce it by turning iCloud Drive off just now and yet it still works fine for me. Search this site with your error text for more pointers as others say you need to enable iCloud Documents under the iCloud capability in your app - although I don't have it enabled either. – OzSimTech Jul 27 '16 at 15:26
  • I turned it on, it was off, but that didn't help. Can you post the code you use to make it work? – Bryan Cimo Aug 01 '16 at 06:38
1

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(@"-----");
          }];

     }];
}
Thunk
  • 4,099
  • 7
  • 28
  • 47
  • Hello, it's a little bit of off topic, but how do you add players to the GKGameSession? I see that you use turn based ui which returns you GKTurnBasedMatch with participants (GKPlayer's), right? And you can't get GKCloudPlayer from it, so... why do you create a session then? (I wanted to do the same and use modern iOS 10+ approach, but confused with matchmaking and sessions thing). Thanks. – Dima Deplov Jun 26 '17 at 01:24