2015-09-13 43 views
6

繼Parse.com教程推送通知,我把這個斯威夫特代碼到我的應用程序didFinishLaunchingWithOptions方法:Parse.com UIRemoteNotificationType已過時

 // Register for Push Notitications 
    if application.applicationState != UIApplicationState.Background { 
     // Track an app open here if we launch with a push, unless 
     // "content_available" was used to trigger a background push (introduced in iOS 7). 
     // In that case, we skip tracking here to avoid double counting the app-open. 

     let preBackgroundPush = !application.respondsToSelector("backgroundRefreshStatus") 
     let oldPushHandlerOnly = !self.respondsToSelector("application:didReceiveRemoteNotification:fetchCompletionHandler:") 
     var pushPayload = false 
     if let options = launchOptions { 
      pushPayload = options[UIApplicationLaunchOptionsRemoteNotificationKey] != nil 
     } 
     if (preBackgroundPush || oldPushHandlerOnly || pushPayload) { 
      PFAnalytics.trackAppOpenedWithLaunchOptions(launchOptions) 
     } 
    } 
    if application.respondsToSelector("registerUserNotificationSettings:") { 
     let userNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound 
     let settings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil) 
     application.registerUserNotificationSettings(settings) 
     application.registerForRemoteNotifications() 
    } else { 
     let types = UIRemoteNotificationType.Badge | UIRemoteNotificationType.Alert | UIRemoteNotificationType.Sound 
     application.registerForRemoteNotificationTypes(types) 
    } 

我得到這兩個警告:

'UIRemoteNotificationType' was deprecated in iOS version 8.0: Use UIUserNotificationType for user notifications and registerForRemoteNotifications for receiving remote notifications instead.

'registerForRemoteNotificationsTypes' was deprecated in iOS version 8.0: Please use registerForRemoteNotifications and registerUserNotificationSettings: instead

我可以簡單地換掉它告訴我的內容嗎?

回答

5

答案是非常黑和白。是的,您可以簡單地將其交換出去,但需要注意:

如果您的設備是iOS 7的設備,那麼不需要。但是,如果您正在開發iOS7 ...我的意見,請停止它。截至8月31日和According to Apple並沒有那麼多用戶在他們的設備上仍然有這個操作系統,而且這些數據甚至沒有包含公共的iOS 9,所以你在沒有人使用的操作系統上浪費了大量的時間。但是,如果您的確實必須支持iOS 7,則除了未棄用的版本之外,您還需要包含所有這些內容。否則,您可以按照您對非棄用版本的說明進行交換。

這裏有一個雨燕2.0例如:

if #available(iOS 8.0, *) { 
    let types: UIUserNotificationType = [.Alert, .Badge, .Sound] 
    let settings = UIUserNotificationSettings(forTypes: types, categories: nil) 
    application.registerUserNotificationSettings(settings) 
    application.registerForRemoteNotifications() 
} else { 
    let types: UIRemoteNotificationType = [.Alert, .Badge, .Sound] 
    application.registerForRemoteNotificationTypes(types) 
} 
+0

感謝您的答覆。我不需要定位iOS 7,所以我用最後幾行代碼替換了:'} else {type = UIUserNotificationType.Badge | UIUserNotificationType.Alert | UIUserNotificationType.Sound application.registerForRemoteNotifications(types) }'但我得到錯誤:無法用類型爲UIUserNotificationType的參數列表調用registerForRemoteNotifications – jordangrogan

+0

我不明白。對於你正在做的iOS8版本:'registerForRemoteNotifications()',但你沒有傳遞任何類型。它如何知道它必須註冊哪些類型? – Honey