10

ターンベースのゲームにイベントリスナーを実装して、プレイヤーが自分のターンがアクティブになったとき、または友人から招待されたときに受け取ることができるようにしようとしていました。GKTurnBasedEventHandler は IOS 7 で廃止され、GKLocalPlayerListener を使用する必要があるというドキュメントを読みました。しかし、それはその延長です。どこにも情報がないので、すでに使っている人はいますか?

これは私が以前に試したもので、うまくいきません。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
    [localPlayer authenticateWithCompletionHandler:^(NSError *error)
     {
         if (localPlayer.isAuthenticated)
         { 
             GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
             [localPlayer registerListener:self];
         }
     }];

    return YES;
}

-(void)handleInviteFromGameCenter:(NSArray *)playersToInvite
{
    NSLog(@"test");
}

- (void)player:(GKPlayer *)player receivedTurnEventForMatch:(GKTurnBasedMatch *)match didBecomeActive:(BOOL)didBecomeActive
{
    NSLog(@"test");
}
4

3 に答える 3

2

GKLocalPlayerListener を登録するために使用するコードを次に示します。

__weak GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error) {
   if (viewController) {
         [authenticateFromViewController presentViewController:viewController animated:YES completion:^{
          [localPlayer registerListener:self];
          NSLog(@"Authenticated. Registering Turn Based Events listener");
        }];
  } else if (localPlayer.authenticated) {
         [localPlayer registerListener:self];
         NSLog(@"User Already Authenticated. Registering Turn Based Events listener");
  } else {
         NSLog(@"Unable to Authenticate with Game Center: %@", [error localizedDescription]);
  }
};

ドキュメントには、GKLocalPlayerEventListener の登録は 1 回だけ行うべきであると記載されているため、既に登録しているかどうかを確認することでこのコードを改善できます。

これauthenticateWithCompletionHandlerは iOS 6 では非推奨であり、上記のように authenticateHandler プロパティを設定することをお勧めします。

于 2013-11-20T00:03:50.600 に答える
1

少し遅れるかもしれませんが、誰かの役に立てば幸いです...

これが私がすることです。Apple のドキュメントによると、[my] アプリに適切な場合に認証ビューを表示する [my] 独自のメソッドを作成します。

    - (void)authenticateLocalUser
    {
        if ([GKLocalPlayer localPlayer].authenticated == NO) {
            __weak typeof(self) weakSelf = self;
            __weak GKLocalPlayer *weakPlayer = [GKLocalPlayer localPlayer];

            weakPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error) {
                if (viewController != nil) {
                    [weakSelf showAuthenticationDialogWhenReasonable:viewController];
                } else if (weakPlayer.isAuthenticated) {
                    // Player has been authenticated!
                    [weakPlayer registerListener:weakSelf];
                } else {
                    // Should disable Game Center?
                }
            };

        } else {
            // Already authenticated
            [[GKLocalPlayer localPlayer] registerListener:self];
        }
    }


    -(void)showAuthenticationDialogWhenReasonable:(UIViewController *)controller
    {
        [[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:controller animated:YES completion:nil];
    }

このコードはシングルトン ヘルパー クラス内にあり、独自のクラスにある場合は単純化される可能性があります。

于 2014-03-19T18:54:41.940 に答える