2014-09-20 40 views
0

對於iOS7,解析處理推送通知,在AppDelegate中下面的代碼:如何根據iOS版本處理推送通知?

[application registerForRemoteNotificationTypes: 
UIRemoteNotificationTypeBadge| 
UIRemoteNotificationTypeAlert| 
UIRemoteNotificationTypeSound]; 

registerForRemoteNotificationTypes在iOS8上不支持然而,和用於處理現在iOS8上的推送通知新的代碼如下所示:

UIUserNotificationSettings *settings = 
[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | 
UIUserNotificationTypeBadge | 
UIUserNotificationTypeSound 
            categories:nil]; 
[[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 
[[UIApplication sharedApplication] registerForRemoteNotifications]; 

在iOS7設備上使用此新代碼會導致應用程序崩潰,因此我需要讓代碼確定手機的版本,然後運行相應的推送通知代碼。我如何讓應用程序檢查這個,並使用正確的?

+0

[registerForRemoteNotificationTypes:在iOS 8.0及更高版本中不受支持]的可能重複(http://stackoverflow.com/questions/24454033/registerforremotenotificationtypes-is-not-supported-in-ios-8-0-and-later ) – 2014-09-21 06:09:32

回答

3

重複它總是更好地檢查的方法的可用性,而不是操作系統版本。

if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerForRemoteNotifications)]) { 

    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]; 

    [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 

    [[UIApplication sharedApplication] registerForRemoteNotifications]; 

} else { 

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; 
} 

假設您的部署目標> = 7.0。

0

可能的registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) 
{ 
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; 
    [[UIApplication sharedApplication] registerForRemoteNotifications]; 
} 
else 
{ 
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: 
    (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)]; 
} 
+1

最好的辦法是不要爲此付出代價就是添加一個帶有其他問題鏈接的評論。或者,更好的是,將這個問題標記爲重複。 – 2014-09-21 06:17:49