iosicloudgame-centergkgamesession

How do I programmatically retrieve the icloud container name?


To create a GKGameSession I need to pass in the iCloud container name.

+ (void)createSessionInContainer:(NSString *)containerName 
                   withTitle:(NSString *)title 
         maxConnectedPlayers:(NSInteger)maxPlayers 
           completionHandler:(void (^)(GKGameSession *session, NSError *error))completionHandler;

Although I know where to find the container name in xCode by navigating to the Capabilities page, and although I know the default format is iCloud.$(CFBundleIdentifier) I'm confused about how I programmatically find the container name.

I don't see it as an entry in the info.plist file. While I could reconstruct it using the default format, what if I don't use the default container name? Do I really have to #definethe string?


Solution

  • To find the default container's ID programmatically:

    #import <CloudKit/CloudKit.h>
    CKContainer *defaultContainer = [CKContainer defaultContainer];
    NSLog(@"default container ID: %@",defaultContainer.containerIdentifier);