Notifications 类型UNUserNotificationCenter没有当前成员,swift 2.3
在iOS10中,用户通知被苹果重新修改。 现在,我正在尝试调整我的应用程序以适应这些更改,如下所示: 给我一个错误:Notifications 类型UNUserNotificationCenter没有当前成员,swift 2.3,notifications,swift2,ios10,Notifications,Swift2,Ios10,在iOS10中,用户通知被苹果重新修改。 现在,我正在尝试调整我的应用程序以适应这些更改,如下所示: 给我一个错误: Type UNUserNotificationCenter has no member current, swift 2.3 我知道那个教程可能是给swift3的。但我需要在swift 2.3中实现。它是否可行?如果可行,怎么做?文档本身似乎存在冲突。虽然它描述了current()方法并说明如何使用它,但示例显示了let center=UNUserNotificationCen
Type UNUserNotificationCenter has no member current, swift 2.3
我知道那个教程可能是给swift3的。但我需要在swift 2.3中实现。它是否可行?如果可行,怎么做?文档本身似乎存在冲突。虽然它描述了
current()
方法并说明如何使用它,但示例显示了let center=UNUserNotificationCenter.currentNotificationCenter()
正如您所说,这可能是一个Swift版本依赖关系。文档本身似乎存在冲突。虽然它描述了
current()
方法并说明如何使用它,但示例显示了let center=UNUserNotificationCenter.currentNotificationCenter()
正如您所说,这可能是一个Swift版本依赖项。对于Xcode 8/ios9/10
只需使用:
import UserNotifications
。
//注意:如果你在前台,你将永远不会被呼叫 对于Xcode 8/ios9/10
只需使用:
import UserNotifications
。
//注意:如果你在前台,你将永远不会被呼叫 导入此:
import UserNotifications
在appDelegate
文件中使用以下命令实现此委托:
UNUserNotificationCenterDelegate
这将允许您访问以下方法:
func userNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification?) {}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {}
然后将其添加到应用程序代理的didfishlaunchwithoptions
方法中,以将其绑定起来
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
导入此文件:
import UserNotifications
在appDelegate
文件中使用以下命令实现此委托:
UNUserNotificationCenterDelegate
这将允许您访问以下方法:
func userNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification?) {}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {}
然后将其添加到应用程序代理的didfishlaunchwithoptions
方法中,以将其绑定起来
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
swift 4.1
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: {_, _ in
self.getNotificationSettings()
})
} else {
// Fallback on earlier versions
let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
}
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
swift 4.1
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: {_, _ in
self.getNotificationSettings()
})
} else {
// Fallback on earlier versions
let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
}
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
你能发一个链接到那些例子吗,因为我现在被困得更远了,原因可能相同?@Async-在线文档:你能发一个链接到那些例子吗,因为我现在被困得更远了,原因可能相同?@Async-在线文档:这是最好的答案。这是最好的答案。