首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

iOS 集成個推啟動崩潰?缺少 remote-notification 背景模式 + CALayer NaN 佈局錯誤雙重修復

     編輯:程式設計綜合問答

集成個推 SDK 後 App 啟動即崩,控制台噴出兩類關鍵錯誤:缺少 remote-notification 背景模式、CALayer position contains NaN 佈局異常。本文給出雙管齊下的修復方案。

錯誤 1:缺少 Background Mode

You've implemented -[application:didReceiveRemoteNotification:fetchCompletionHandler:],
but you still need to add "remote-notification" to the list of your supported UIBackgroundModes

修復:Info.plist 添加鍵值

<key>UIBackgroundModes</key>
<array>
    <string>remote-notification</string>
</array>

或在 Xcode → Target → Capabilities → Background Modes 勾選 Remote notifications。

錯誤 2:CALayer 位置為 NaN

*** Terminating app due to uncaught exception 'CALayerInvalidGeometry',
reason: 'CALayer position contains NaN: [nan nan]'

根因排查

  • 某個 View 的 frame / bounds 計算出現 NaN(如除以零、未初始化的 CGFloat)。
  • 個推 SDK 內部可能在啟動時彈窗/顯示浮層,觸發佈局計算,放大了宿主 App 既有的佈局 Bug。

快速定位法

  1. 啟用 View Debugging(Xcode → Debug → View Debugging → Capture View Hierarchy)。
  2. 在崩潰堆疊中找到首個自有代碼幀(如 0x1661b3),設置斷點檢查對應 View 的 frame。
  3. 補齊 if (CGRectIsNull(rect) || isnan(rect.origin.x)) return; 防禦性判斷。

最小可行修復代碼

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 1. 註冊遠程通知(iOS 10+)
    if (@available(iOS 10.0, *)) {
        UNUserNotificationCenter.currentNotificationCenter.delegate = self;
        [UNUserNotificationCenter.currentNotificationCenter requestAuthorizationWithOptions:UNAuthorizationOptionAlert|UNAuthorizationOptionSound|UNAuthorizationOptionBadge completionHandler:nil];
    } else {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound|UIUserNotificationTypeBadge categories:nil];
        [application registerUserNotificationSettings:settings];
    }
    [application registerForRemoteNotifications];
    
    // 2. 初始化個推
    [GeTuiSdk startSdkWithAppId:kGtAppId delegate:self];
    return YES;
}

// 兼容舊版回調(若不需要 fetchCompletionHandler 可直接刪除該方法)
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    [GeTuiSdk handleRemoteNotification:userInfo];
}

加上 Background Mode 並修正佈局 NaN 後,App 可正常啟動並接收推送。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved