【问题标题】:ios7 app crashes when i press home button and then resume it当我按下主页按钮然后恢复它时,ios7应用程序崩溃
【发布时间】:2014-07-22 21:16:05
【问题描述】:

这是我收到的错误消息: 由于未捕获的异常“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;

    }

【问题讨论】:

  • 您能否在您的问题中添加 authenticationChanged 方法?
  • 那是非常糟糕的代码。对不起,我很抱歉,但我建议你回到你的基础。
  • ups,我粘贴时代码格式不正确。已编辑。

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


【解决方案1】:

此问题的最可能原因是您从未在需要时移除观察者。

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

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

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

附带说明,不要重复代码。您的authenticationChanged 方法会更好:

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

并确保将YESNOBOOL 变量一起使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多