iOS8 PUSH解決方案

來源:互聯網
上載者:User

標籤:blog   http   io   ar   os   使用   for   sp   div   

本文轉載至 http://blog.csdn.net/pjk1129/article/details/39548523  

- (void)registerForRemoteNotificationTypes:(UIRemoteNotificationType)types NS_DEPRECATED_IOS(3_0, 8_0, "Please use registerForRemoteNotifications and registerUserNotificationSettings: instead")


昨天晚上整理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  轉載請註明!!

iOS8 PUSH解決方案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.