在appDelegate中包含此文件如下:
// 需要包含这个文件
import UserNotifications
具体如下
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// #available(iOS 10.0, *)判断版本(Xcode8智能提示会给出)
if #available(iOS 10.0, *) {
// 10.0版本以后用这个方法
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .carPlay, .sound]) { (success, error) in
}
} else {
// 10.0版本以前用这个方法
let notifySettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(notifySettings)
}
return true
}
注意:iOS10以后多了carPlay的请求,总共请求4项
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。