iOS8 PUSH解決方法
阿新 • • 發佈:2019-01-03
昨天晚上整理PUSH的東西,準備些一個教程,全部弄好之後,發現沒有達到預期的效果,本以為是伺服器程式碼的問題(因為本人對PHP程式碼一點都不懂),所以在網上四處搜尋,後來看xcode log才發現,原來是IOS8系統更新了的問題,提示 registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later.
使用IOS8 xcode6的同學,在使用推送(push)的時候應該已經出現這個問題了。那麼讓我們來看看具體的解決方法。
iOS 8 has changed notification registration in a non-backwards compatible way. While you need to support iOS 7 and 8 (and while apps built with the 8 SDK aren't accepted), you can check for the selectors you need and conditionally call them correctly for the running version.
Here's a category on UIApplication that will hide this logic behind a clean interface for you that will work in both Xcode 5 and Xcode 6.
// IOS8 新系統需要使用新的程式碼咯
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)];
}
原本在IOS7當中 判斷PUSH是否開啟的方法是:
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
return (types & UIRemoteNotificationTypeAlert);
如果將這段程式碼使用在 IOS當中,雖然不會出現crash的現象,但是基本沒什麼作用。
在IOS8中,我們使用如下的新程式碼來取代以上的程式碼
{
UIRemoteNotificationType types;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
types = [[UIApplication sharedApplication] currentUserNotificationSettings].types;
}
else
{
types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
}
return (types & UIRemoteNotificationTypeAlert);
}
每當蘋果更新一個新的版本的時候,最痛苦的莫過於我們這群屌絲啊
加油碼農!
本文轉自 http://www.999dh.net/home.php?mod=space&uid=1&do=blog&quickforward=1&id=419 轉載請註明!!