swa*_*ner 10 apple-push-notifications ios7 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
}
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)
Leo*_*ica 15
您的代码仅添加了对旧版Xcode和iOS SDK的编译支持.
您应该在运行时添加以下检查:
#ifdef __IPHONE_8_0
if(NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) {
[sharedApplication registerForRemoteNotifications]; // <--- CRASH HERE
}
else
#endif
{
[sharedApplication registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
}
Run Code Online (Sandbox Code Playgroud)
ifdef是在编译(或更确切地说是预处理)时计算的,而不是在运行时,因此构建的二进制文件中包含的两个registerForRemoteNotifications调用中的哪一个仅取决于您构建的SDK,而不是它运行的设备.
| 归档时间: |
|
| 查看次数: |
8353 次 |
| 最近记录: |