簡體   English   中英

當我按下主頁按鈕然后恢復它時,ios7應用程序崩潰

[英]ios7 app crashes when i press home button and then resume it

這是我得到的錯誤消息: 由於未捕獲的異常'NSInvalidArgumentException'而終止應用程序,原因:'-[__ NSCFString authenticationChanged]:無法識別的選擇器已發送到實例0x176769a0'第一個調用堆棧:(0x30496ecb 0x3ac31ce7 0x3049a7f7 0x303e80b545814583f1 0x30e3b43f3 0x3d3d3f4e 0x303e808f3e0f0f0 0x30e3b3e5e0f0f0 0x303e808f3f0 0e0f0 0f0 0e0f0 0f0 0e0 0b0 0f0 0f0 0f0 0e0 0b0 0f0 0 0f0 0 0 0 0 0 0 0 0 0 0 0 0 0f 0 0 0 0 0 0 0b 0 0 0 0 0 0 0 0 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;

    }

造成此問題的最可能原因是,您永遠不會在需要時刪除觀察者。

將以下內容添加到您的課程中:

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

這樣可以確保不再注冊舊對象以接收通知。

附帶說明,不要重復代碼。 您的authenticationChanged方法最好是:

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

並且確保對BOOL變量使用YESNO

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM