Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 游戏中心身份验证,无法取消登录viewController_Ios_Objective C_Login_Uiviewcontroller_Game Center - Fatal编程技术网

Ios 游戏中心身份验证,无法取消登录viewController

Ios 游戏中心身份验证,无法取消登录viewController,ios,objective-c,login,uiviewcontroller,game-center,Ios,Objective C,Login,Uiviewcontroller,Game Center,我正在为iOS编写一个游戏应用程序,可以选择使用带有排行榜的游戏中心来存储高分。我想要实现的是: 应用程序首次启动时,请显示游戏中心登录viewController 如果用户成功登录,一切正常 如果用户取消登录过程,请不要再打扰他(即使是在下一个应用程序启动时) 如果用户选择需要Game Center的功能,请给他一个登录的机会 内置的authenticateHandler不会捕获secong登录尝试(已报告),因此如果登录viewController之前被取消一次,则不会再次出现。因此,我

我正在为iOS编写一个游戏应用程序,可以选择使用带有排行榜的游戏中心来存储高分。我想要实现的是:

  • 应用程序首次启动时,请显示游戏中心登录viewController
  • 如果用户成功登录,一切正常
  • 如果用户取消登录过程,请不要再打扰他(即使是在下一个应用程序启动时)
  • 如果用户选择需要Game Center的功能,请给他一个登录的机会
内置的authenticateHandler不会捕获secong登录尝试(已报告),因此如果登录viewController之前被取消一次,则不会再次出现。因此,我尝试去保存对登录viewController的引用,并在适当的时候显示它。按下“我的高分”按钮时,登录viewController将按预期显示。当用户取消登录时,viewController将被解除(但只是第一次)。这意味着,如果他第二次按下按钮,登录viewController将再次出现,但不能再通过按下左上角的取消按钮来取消

看看我的代码:

@interface GameKitHelper () {
    UIViewController __weak *presentingVC;
}
@property (nonatomic, strong) NSArray *leaderBoards;
@property (nonatomic, strong) completionBlock completion;
@property (nonatomic, strong) UIViewController *gcLoginVC;
@end

- (void) authenticateLocalPlayerForce: (BOOL) force completion: (void (^)(BOOL)) completion {
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
    if (localPlayer.authenticated && completion) {
        completion(localPlayer.authenticated);
        return;
    }
    self.completion = completion;
    if (self.gcLoginVC && !localPlayer.authenticated && [[NSUserDefaults standardUserDefaults] boolForKey: CUserDeactivatedGC] && force) {
        [self presentViewController: self.gcLoginVC force: force];
        return;
    }
    localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error) {

        [self setLastError: error];

        if ([GKLocalPlayer localPlayer].authenticated) {
            [[NSUserDefaults standardUserDefaults] setBool: NO forKey: CUserDeactivatedGC];
            if (self.completion) {
                self.completion([GKLocalPlayer localPlayer].authenticated);
            }
            return;
        }
        if (viewController) {
            self.gcLoginVC = viewController;
            [self presentViewController: viewController force: force];
            return;
        }
        [[NSUserDefaults standardUserDefaults] setBool: YES forKey: CUserDeactivatedGC];
        [[NSUserDefaults standardUserDefaults] synchronize];
    };
}
//在应用程序启动时,不要强制显示登录viewController

- (void) viewDidLoad {
    [super viewDidLoad];
    [[GameKitHelper sharedGameKitHelper] authenticateLocalPlayerForce: NO completion:^(BOOL authenticated) {
        if (authenticated) {
            [[GameKitHelper sharedGameKitHelper] loadLeaderBoards];
        }
    }];
}
- (IBAction) highscoresButtonTapped: (UIButton *) sender {
    [[GameKitHelper sharedGameKitHelper] authenticateLocalPlayerForce: YES completion:^(BOOL authenticated) {
        if (authenticated) {
            SGGameLeaderboardVC *vc = [[SGGameLeaderboardVC alloc] init];
            [self.navigationController pushViewController: vc animated: YES];
        }
    }];
}
//当按下highscores(高分)按钮时,强制显示登录视图控制器

- (void) viewDidLoad {
    [super viewDidLoad];
    [[GameKitHelper sharedGameKitHelper] authenticateLocalPlayerForce: NO completion:^(BOOL authenticated) {
        if (authenticated) {
            [[GameKitHelper sharedGameKitHelper] loadLeaderBoards];
        }
    }];
}
- (IBAction) highscoresButtonTapped: (UIButton *) sender {
    [[GameKitHelper sharedGameKitHelper] authenticateLocalPlayerForce: YES completion:^(BOOL authenticated) {
        if (authenticated) {
            SGGameLeaderboardVC *vc = [[SGGameLeaderboardVC alloc] init];
            [self.navigationController pushViewController: vc animated: YES];
        }
    }];
}

presentViewController不应该由viewController调用吗?另外,您可能希望尝试向presentViewController添加一个完成块,该块执行[self-dismissViewControllerAnimated:YES completion:nil];顺便说一句,我正试图解决您的第一个问题,当用户按下排行榜按钮时,需要进行GC身份验证。我从未想过缓存传入的viewController。你知道苹果公司的做法是否符合犹太教。他们会将其标记为问题并通过证书吗?我尝试了你的方法并遇到了相同的问题。显然,缓存传入的视图控制器似乎无法保持其有效性。第二次出现时,我无法登录或取消。