ios7 - #ifdef __IPHONE_8_0代码也可在iOS 7上运行

标签 ios7 apple-push-notifications ios8

我的应用程序中包含以下代码-并且在注释行中看到iOS 7崩溃。

+ (void)registerDeviceForRemoteNotifications {
#if !TARGET_IPHONE_SIMULATOR
    if ([[KOAAuthenticator sharedAuthenticator] currentUser] != nil) {
        UIApplication *sharedApplication = [UIApplication sharedApplication];
#ifdef __IPHONE_8_0
        [sharedApplication registerForRemoteNotifications]; // <--- CRASH HERE
#else
        [sharedApplication registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
#endif
    }
#endif
}

Crashlytics说:-[UIApplication registerForRemoteNotifications]: unrecognized selector sent to instance 0x157d04290
那怎么可能呢?不应在iOS 7上调用此代码,对吗?

编辑:解决方案
+ (void)registerDeviceForRemoteNotifications {
#if !TARGET_IPHONE_SIMULATOR
    if ([[KOAAuthenticator sharedAuthenticator] currentUser] != nil) {

        UIApplication *sharedApplication = [UIApplication sharedApplication];

#ifdef __IPHONE_8_0
        if ([sharedApplication respondsToSelector:@selector(registerForRemoteNotifications)]) {
            [sharedApplication registerForRemoteNotifications];
        } else {
            [sharedApplication registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
        }
#else
        [sharedApplication registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
#endif

    }
#endif
}

最佳答案

您的代码仅增加了对在较旧版本的Xcode和iOS SDK上进行编译的支持。

您应该在运行时添加以下检查:

#ifdef __IPHONE_8_0
        if(NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) {
            [sharedApplication registerForRemoteNotifications]; // <--- CRASH HERE
        }
        else
#endif
        {
            [sharedApplication registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
        }

关于ios7 - #ifdef __IPHONE_8_0代码也可在iOS 7上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25301751/

相关文章:

ios - 无法在 IOS6 中将 UILable 添加到 UITableViewCell

ios - 在 iOS 7 中显示图像,但在 iOS 6.1 中不显示

iphone - 为什么我无法连接到苹果推送通知服务?

ios - Apple 推送通知 : Not receiving device token?

ios - 在 iOS8 中的 App 和 Extension 之间共享核心数据堆栈和数据

ios - 启用位码是否适用于 iOS 9(iOS 7/8)之前的 iOS 版本?

ios - 在状态栏上方添加 UIView 横幅 iOS 7

ios - iphone 锁定时的声音和振动通知

video-streaming - 从 CMSampleBufferRef 获取 sps 和 pps

ios - 创建嵌套 TableView 或单独的页面?