#ifdef __IPHONE_8_0 代码也在 iOS 7 上运行 [英] #ifdef __IPHONE_8_0 code runs also on iOS 7

查看:23
本文介绍了#ifdef __IPHONE_8_0 代码也在 iOS 7 上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用中有以下代码 - 我在 iOS 7 上看到了一些与评论相关的崩溃.

I've got the following code in my app - and I see some crashes on iOS 7 in the line with the comment.

+ (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]:无法识别的选择器发送到实例 0x157d04290

Crashlytics says: -[UIApplication registerForRemoteNotifications]: unrecognized selector sent to instance 0x157d04290

这怎么可能?此代码不应在 iOS 7 上调用,对吗?

how's that even possible? This code shouldn't be called on iOS 7, right?

解决方案

+ (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 上编译的支持.

Your code only adds support for compiling on an older version of Xcode and the iOS SDK.

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

You should add the following checks at runtime:

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

这篇关于#ifdef __IPHONE_8_0 代码也在 iOS 7 上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆