UIUserNotificationSettings settingsForTypes:崩溃的iOS7设备

时间:2015-06-16 22:46:06

标签: ios objective-c notifications push-notification apple-push-notifications

出于某种原因,每当我尝试在iOS 7设备上注册远程通知时,我的应用程序立即崩溃。我正在运行以下代码块:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    }
#else
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
#endif

哪个应该允许我在iOS 8和7中注册通知而没有任何障碍(作为我已经发现并查看过的一些示例)但无论出于何种原因它仍然崩溃。我已将崩溃范围缩小到我发布的第三行代码 - 更具体地说是以下语句:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil];如果我将其设置为nil该应用程序运行完全正常,但如果我离开它的线刚刚开始崩溃。

我已经尝试在respondsToSelector if语句中放入一个日志,但代码肯定没有在iOS 7设备上运行,所以我有点不知道该做什么。

修改 经过几个小时的故障排除后,我终于找到了解决方案。我需要将 UIKit.framework 导入到Linked Frameworks和Libraries下的项目中,并将其设置为可选

1 个答案:

答案 0 :(得分:0)

我不知道#if,因为我从未使用它们,但我有一个非常相似的代码,所以你可以试试这个:

if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    // use registerUserNotificationSettings
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

    [[UIApplication sharedApplication] registerForRemoteNotifications];

} else {
    // use registerForRemoteNotificationTypes:
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}

如果这对你不起作用,也许还有其他因素导致崩溃..忍者式......