iOS Developer

iOS 10以后Swift 3.1怎么请求用户授权通知?

2017-04-22  本文已影响47人  韩旭杰

在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项

上一篇 下一篇

猜你喜欢

热点阅读