2014-09-20 67 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