【问题标题】:UIUserNotificationSettings settingsForTypes: crashing iOS7 DevicesUIUserNotificationSettings settingsForTypes:使 iOS7 设备崩溃
【发布时间】:2015-06-16 22:46:06
【问题描述】:

出于某种原因,每当我尝试在 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 and Libraries 下的项目中,并将其设置为 optional

【问题讨论】:

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


    【解决方案1】:

    我不知道#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)];
    }
    

    如果这对您不起作用,则可能是其他原因导致崩溃.. ninja-style..

    【讨论】:

    • 是的,我已经尝试删除所有 #if 代码,因为此时我只在 Xcode 6 中工作,并且使用的正是您发布的内容,但它仍然崩溃。
    • @ImpurestClub 嗯..奇怪。你在哪里叫这个?里面didFinishLaunching..?
    • 是的!我打电话给didFinishLaunchingWithOptions:
    • @ImpurestClub 你尝试复制我的确切代码并使用它?还是什么都没有?
    • 仍然没有(使用您的确切代码)。我觉得它必须与其他东西有关。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    相关资源
    最近更新 更多