最近在開發過程中出現了一個問題,就是在iOS8.0系統上,我的應用在系統的設置->通知中心中不見了。這也就意味着系統認爲我沒有註冊通知的需要,或者我的註冊是失敗的。
過去,我們一直使用這個方法進行通知中心的註冊,並且一直沒有出過問題。
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge];
但是在新的文檔中對於該段代碼的解釋卻是這樣的:
- (void)registerForRemoteNotificationTypes:(UIRemoteNotificationType)types NS_DEPRECATED_IOS(3_0, 8_0, "Please use registerForRemoteNotifications and registerUserNotificationSettings: instead");
這也就是說認爲它是Deprecated(過期)了的,並且推薦了新的API進行註冊。於是我們可以根據蘋果的文檔進行重新編寫:(注意區分系統版本,否則會引起崩潰)
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)];
}