热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

iphone_8_0代码也运行在ios7上。-#ifdef__IPHONE_8_0coderunsalsooniOS7

Ivegotthefollowingcodeinmyapp-andIseesomecrashesoniOS7inthelinewiththecomment

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

我在我的应用程序中有以下代码——我看到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 says: -[UIApplication registerForRemoteNotifications]: unrecognized selector sent to instance 0x157d04290

Crashlytics说:-[UIApplication registerforremotenoti]:未识别的选择器发送到实例0x157d04290。

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

这是怎么可能?这段代码不应该在ios7上调用,对吧?

EDIT: solution

编辑:解决方案

+ (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
}

2 个解决方案

#1


15  

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

您的代码只会增加对旧版本Xcode和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];
        }

#2


5  

An ifdef is evaluated at compile (or rather, preprocessing) time, not at runtime, so which one of the two registerForRemoteNotifications calls gets included in your built binary only depends on which SDK you build with, not what device it's run on.

ifdef在编译时(或更确切地说是预处理)时进行评估,而不是在运行时,因此,在构建的二进制文件中包含的两个registerforremotenoti调用中,哪一个只取决于您构建的SDK,而不是它运行的设备。


推荐阅读
author-avatar
奇力0_843
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有