当我按下主页按钮然后恢复它时,ios7应用程序崩溃

时间:2014-07-22 21:16:06

标签: ios ios7 crash game-center unrecognized-selector

这是我收到的错误消息:  由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' - [__ NSCFString authenticationChanged]:无法识别的选择器发送到实例0x176769a0' 第一次抛出调用堆栈: (0x30496ecb 0x3ac31ce7 0x3049a7f7 0x304990f7 0x303e8058 0x30458f01 0x303ccd69 0x30db8cc5 0x3102f43b 0x3b11ad53 0x3b11ad3f 0x3b11d6c3 0x30461641 0x3045ff0d 0x303ca729 0x303ca50b 0x353396d3 0x32d2b871 0xb8591 0x3b12fab7) libc ++ abi.dylib:以NSException类型的未捕获异常终止

我将游戏中心集成到应用程序中,这是可能导致崩溃的代码:

- (id)init {
    if ((self = [super init])) 
    {
        gameCenterAvailable = [self isGameCenterAvailable];
        if (gameCenterAvailable) {
            NSNotificationCenter *nc =
            [NSNotificationCenter defaultCenter];
            [nc addObserver:self
                   selector:@selector(authenticationChanged)
                       name:GKPlayerAuthenticationDidChangeNotificationName
                     object:nil];
        }
    }

    return self;
}

- (void)authenticationChanged {
    if ([GKLocalPlayer localPlayer].isAuthenticated && !userAuthenticated) {
        NSLog(@"Authentication changed: player authenticated.");
        userAuthenticated = TRUE;

    } else if (![GKLocalPlayer localPlayer].isAuthenticated && userAuthenticated) {
        NSLog(@"Authentication changed: player not authenticated");
        userAuthenticated = FALSE;

    }

1 个答案:

答案 0 :(得分:2)

此问题的最可能原因是您永远不会在需要时删除观察者。

将以下内容添加到您的课程中:

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

这可确保不再注册旧对象以接收通知。

在旁注中,不要重复代码。您的authenticationChanged方法会更好:

- (void)authenticationChanged {
    if ([GKLocalPlayer localPlayer].isAuthenticated) {
        userAuthenticated = !userAuthenticated;
        NSLog(@"Authentication changed: player %@authenticated.", userAuthenticated ? @"" : @"not ");
    }
}

请务必将YESNOBOOL个变量一起使用。