1

我正在尝试启动一个新的 GKGameSession,当我使用 createSession 时,到目前为止我所能得到的只是零。这是我的代码:

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)")
        })
    }

它唯一打印的是“nil”。任何帮助表示赞赏。

4

4 回答 4

3

如果您使用模拟器,我建议使用设备。GKGameSessions 不能很好地与模拟器配合使用,因为它们依赖于推送通知和登录的 iCloud 帐户。

于 2016-11-12T22:10:13.637 回答
2

newGameSession是可选的。因此,在创建新会话时似乎出现了问题。

我会说newGameSession很可能nil,在那种情况下error,希望会包含一些有用的信息。

尝试替换print("\(newGameSession)")print(newGameSession, error)查看errorvar 必须说的内容,或者如果您知道该怎么做,请设置断点。

于 2016-07-11T07:20:33.503 回答
1

我有点晚了,但我会三重检查您用于创建会话的容器名称。

我在 xCode 中启用了所有三个选项:键值存储、iCloud 文档和 CloudKit。

我不使用 iCloud 驱动器。

每次我发送邀请时,以下代码都会成功创建一个新会话。它打印新会话,然后遍历该容器 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(@"-----");
          }];

     }];
}
于 2016-10-08T21:15:15.117 回答
1

尝试使用您应用的 iCloud 容器名称而不是“测试”。如果您选择了默认容器选项,容器名称将采用 iCloud.com.yourcompany.appname 格式。为确保您的应用具有 iCloud 容器,您需要在应用的功能中启用它。

于 2016-07-15T08:46:51.973 回答