菜鸟教程小白 发表于 2022-12-13 07:04:56

IOS游戏中心: How to know when default sign in form for game center is finished?


                                            <p><p>我想知道如何知道用户何时完成了 Game Center 登录表单。我正在自动登录 Facebook,但我需要等待 Game Center 登录完成。有什么办法知道吗?</p>

<pre><code>-(void) authenticateLocalPlayer {

    GKLocalPlayer* localPlayer = ;

    localPlayer.authenticateHandler =
    ^(UIViewController *viewController,
      NSError *error) {

      ;

      if (localPlayer.authenticated) {
            _gameCenterFeaturesEnabled = YES;
            NSLog(@&#34;local Player Info: %@&#34;,localPlayer);
            [ setGameCenterId:localPlayer.playerID];
            [ setUserName:localPlayer.alias];
            ;
      } else if(viewController) {

            ;
      } else {
            _gameCenterFeaturesEnabled = NO;
      }
    };
}
-(void) setLastError:(NSError*)error {
    _lastError = ;
    if (_lastError) {
      NSLog(@&#34;GameKitHelper ERROR: %@&#34;, [
                                           description]);
    }
}
-(UIViewController*) getRootViewController {
    return [UIApplication
            sharedApplication].keyWindow.rootViewController;
}

-(void)presentViewController:(UIViewController*)vc {
    UIViewController* rootVC = ;
    ;
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这是来自 <code>authenticateHandler</code> 文档的引用</p>

<blockquote>
<p>The authenticate handler will be called whenever the authentication
process finishes or needs to show UI. The handler may be called
multiple times. Authentication will happen automatically when the
handler is first set and whenever the app returns to the foreground.<br/>
If the authentication process needs to display UI the viewController
property will be non-nil. Your application should present this view
controller and continue to wait for another call of the
authenticateHandler.The view controller will be dismissed
automatically.</p>

<p>Possible reasons for error:<br/>
1. Communications problem<br/>
2. User credentials invalid<br/>
3. User cancelled</p>
</blockquote>

<p>所以 block 会被多次调用,
例如,如果用户没有登录,一个 ViewController 将被传递给这个 block ,在你展示它并且用户提交表单之后,他的 block 将被再次执行。<br/>
这是处理身份验证的代码:</p>

<pre><code>- (void)authenticateLocalPlayer
{
    GKLocalPlayer *localPlayer = ;

    __weak typeof(localPlayer) weakLocalPlayer = localPlayer;
    localPlayer.authenticateHandler =
    ^(UIViewController *viewController, NSError *error)
    {
      if (error) {
            // Something happened, handle it...
            return;
      }
      __strong typeof(weakLocalPlayer) strongLocalPlayer = weakLocalPlayer;
      if (viewController) {
            // Just show it, user needs to submit the form
            return;
      }
      if (strongLocalPlayer.isAuthenticated) {
            // User completed login, do FB login
      } else {
            // GameKit is disabled, show guide to enable it
      }
    };
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于IOS游戏中心: How to know when default sign in form for game center is finished?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/30129062/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/30129062/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: IOS游戏中心: How to know when default sign in form for game center is finished?