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

标签 ios objective-c game-center

我想知道如何知道用户何时完成 Game Center 登录表单。我正在自动登录 Facebook,但我需要等待 Game Center 登录完成。有什么方法可以知道吗?

-(void) authenticateLocalPlayer {

    GKLocalPlayer* localPlayer = [GKLocalPlayer localPlayer];

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

        [self setLastError:error];

        if (localPlayer.authenticated) {
            _gameCenterFeaturesEnabled = YES;
            NSLog(@"local Player Info: %@",localPlayer);
            [[UserManager sharedInstance] setGameCenterId:localPlayer.playerID];
            [[UserManager sharedInstance] setUserName:localPlayer.alias];
            [self retrieveFriends];
        } else if(viewController) {

            [self presentViewController:viewController];
        } else {
            _gameCenterFeaturesEnabled = NO;
        }
    };
}
-(void) setLastError:(NSError*)error {
    _lastError = [error copy];
    if (_lastError) {
        NSLog(@"GameKitHelper ERROR: %@", [[_lastError userInfo]
                                           description]);
    }
}
-(UIViewController*) getRootViewController {
    return [UIApplication
            sharedApplication].keyWindow.rootViewController;
}

-(void)presentViewController:(UIViewController*)vc {
    UIViewController* rootVC = [self getRootViewController];
    [rootVC presentViewController:vc animated:YES completion:nil];
}

最佳答案

这里引用自 authenticateHandler 文档

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.
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.

Possible reasons for error:
1. Communications problem
2. User credentials invalid
3. User cancelled

所以这个block会被多次调用, 例如,如果用户没有登录,一个 View Controller 将被传递到这个 block ,在你呈现它并且用户提交表单之后,他的 block 将再次执行。
这是处理身份验证的代码:

- (void)authenticateLocalPlayer
{
    GKLocalPlayer *localPlayer = [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
        }
    };
}

关于IOS 游戏中心 : How to know when default sign in form for game center is finished?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30129062/

相关文章:

iphone - 无论我做什么,TBXML 根元素都是空的

ios - Xcode 10.1 归档时不显示 'distribute app' 选项,无法归档

ios - ShareKit分享时在iPad上打开Official Facebook App?

objective-c - 如何在 swift 中使用 RACObserve

iphone - GKTurnBasedMatch matchData 的最大大小

ios - 无法在应用商店上传应用

ios - 自动触发滚动两个不同尺寸的uiwebview

objective-c - networkextension 不导入较新的版本

iphone - GameKit 框架有错误或过时?

iphone - Xcode 4 : how to implement game center achievements?