在iOS10中,苹果对用户通知进行了修改。现在我正在尝试调整我的应用程序以适应这些变化,遵循this
let center = UNUserNotificationCenter.current()给我一个错误:
Type UNUserNotificationCenter has no member current, swift 2.3我知道该教程可能是为swift3编写的。但我需要让它在swift 2.3中工作。这是可行的吗?如果可行,怎么做?
发布于 2016-08-03 20:33:24
文档似乎与其本身相冲突。尽管它描述了current()方法并说明要使用它,但示例中显示的是let center = UNUserNotificationCenter.currentNotificationCenter()。
正如您所说,这可能是Swift版本的依赖项。
发布于 2016-08-18 17:00:41
对于Xcode 8/ ios9/10
只需使用:
import UserNotifications..。..。
// swift 3.0:
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
} else {
// Fallback on earlier versions
}//注意:如果你在前台,你永远不会被调用!:)
发布于 2018-11-20 06:46:35
导入以下内容:
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) {}然后将其添加到您的应用程序委托的didFinishLaunchWithOptions方法中,以将其全部绑定在一起。
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()https://stackoverflow.com/questions/38743628
复制相似问题