#ifdef __IPHONE_8_0 код запускается также на iOS 7
У меня есть следующий код в моем приложении - и я вижу некоторые сбои на 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, правильно?
EDIT: решение
+ (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
}
Ответы
Ответ 1
В вашем коде добавлена поддержка для компиляции более старой версии Xcode и iOS SDK.
Во время выполнения вы должны добавить следующие проверки:
#ifdef __IPHONE_8_0
if(NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) {
[sharedApplication registerForRemoteNotifications]; // <--- CRASH HERE
}
else
#endif
{
[sharedApplication registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
}
Ответ 2
Идентификатор ifdef оценивается при компиляции (или, скорее, предварительной обработке) времени, а не во время выполнения, поэтому один из двух вызовов registerForRemoteNotifications, включенных в ваш встроенный двоичный файл, зависит только от того, с каким SDK вы строите, а не с каким устройством, на котором оно выполняется.