按照推送通知的Parse.com教程,我把这个Swift代码放到我的应用程序didFinishLaunchingWithOptions方法中:
// 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)
}
我收到了这两个警告:
iOS版本8.0中不推荐使用'UIRemoteNotificationType':使用UIUserNotificationType进行用户通知,使用registerForRemoteNotifications接收远程通知。
iOS版本8.0中不推荐使用'registerForRemoteNotificationsTypes':请使用registerForRemoteNotifications和registerUserNotificationSettings:而不是
我可以简单地换掉它告诉我的内容吗?
答案 0 :(得分:5)
答案非常黑白分明。是的,您可以简单地将其换掉,但需要注意一点:
如果你的目标设备是iOS 7,那么你就不需要了。但是,如果您正在为iOS7开发...我的意见,请停止它。截至8月31日和According to Apple,并没有那么多用户在他们的设备上仍然拥有此操作系统,并且该数据甚至不包括公共iOS 9,因此您在操作系统上浪费了大量时间没人用。但是,如果确实必须支持iOS 7,除了不推荐使用的版本之外,还需要包含所有这些内容。否则,你可以像你所说的那样用非弃用的版本交换它。
这是一个Swift 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)
}