美文网首页
[002][iOS-Swift]使用UISwitch控制本地消息

[002][iOS-Swift]使用UISwitch控制本地消息

作者: 快乐捣蛋鬼 | 来源:发表于2019-06-18 11:37 被阅读0次

    (完整代码见文末)

    屏幕快照 2019-06-18 下午12.56.39.png

    在viewWillAppear 中调用checkNotification(),检查通知,并使 switch 的状态对应

        override func viewWillAppear(_ animated: Bool) {
            self.checkNotification()
        }
        
        @objc func checkNotification() {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (accept, error) in
                if !accept {
                    DispatchQueue.main.async {
                        self.notificationSwitch.isOn = false
                    }
                } else {
                    DispatchQueue.main.async {
                        self.notificationSwitch.isOn = true
                    }
                }
            }
        }
    
    屏幕快照 2019-06-18 下午12.56.51.png

    如果当前 switch 是开着的,点击之后,立马变为关的状态,所以下面的判断是如果 switch 是关的状态时有个Alert提示框。

       @IBAction func switchNotification(_ sender: UISwitch) {
          
           if notificationSwitch.isOn == false {
               // 一个Alert提示框,用户点击确认之后再跳转到消息通知界面
               let alertController = UIAlertController(title: "温馨提示", message: "确认关闭消息通知?关闭消息通知将影响App的使用", preferredStyle: .alert)
               let cancelAction = UIAlertAction(title: "取消", style: .cancel) { (cancel) in
                   self.notificationSwitch.isOn = !self.notificationSwitch.isOn
               }
               
               let confirmAction = UIAlertAction(title: "确认", style: .default) { (okaction) in
                   // 跳转到消息通知界面
                   let url = URL(string: UIApplication.openSettingsURLString)
                   if let url = url, UIApplication.shared.canOpenURL(url) {
                       if #available(iOS 10, *) {
                           UIApplication.shared.open(url, options: [:], completionHandler: { (success) in
                           })
                       } else {
                           UIApplication.shared.openURL(url)
                       }
                   }
                   
               }
               alertController.addAction(cancelAction)
               alertController.addAction(confirmAction)
               self.present(alertController, animated: true, completion: nil)
           } else {
                // 直接转到开启通知界面
               let url = URL(string: UIApplication.openSettingsURLString)
               if let url = url, UIApplication.shared.canOpenURL(url) {
                   if #available(iOS 10, *) {
                       UIApplication.shared.open(url, options: [:], completionHandler: { (success) in
                           
                       })
                   } else {
                       UIApplication.shared.openURL(url)
                   }
               }
           }
           
       }
    
    屏幕快照 2019-06-18 下午12.57.05.png

    跳转到系统设置的当前 testproject app设置之后,点击通知。

    屏幕快照 2019-06-18 下午12.57.18.png

    开启或者关闭通知之后,点击左上角返回到 testproject app。
    由于从系统设置返回不会调用 vc 生命周期的方法, 在 viewDidLoad 以及 viewWillAppear 检查是否开启通知,更新 switch 的状态是无效的,所以添加一个 observer,当 app didBecomeActive时,再调用checkNotification(),刷新界面

       override func viewDidLoad() {
           super.viewDidLoad()
          
           NotificationCenter.default.addObserver(self, selector: #selector(checkNotification), name: UIApplication.didBecomeActiveNotification, object: nil)
       }
    
       @objc func checkNotification() {
           UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (accept, error) in
               if !accept {
                   DispatchQueue.main.async {
                       self.notificationSwitch.isOn = false
                   }
               } else {
                   DispatchQueue.main.async {
                       self.notificationSwitch.isOn = true
                   }
               }
           }
       }
    

    完整代码

    import UIKit
    import UserNotifications
    
    class NotificationViewController: UIViewController {
    
        @IBOutlet weak var notificationSwitch: UISwitch!
        
       
        override func viewDidLoad() {
            super.viewDidLoad()
           
            NotificationCenter.default.addObserver(self, selector: #selector(checkNotification), name: UIApplication.didBecomeActiveNotification, object: nil)
        }
        
        override func viewWillAppear(_ animated: Bool) {
            self.checkNotification()
        }
     
        @objc func checkNotification() {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (accept, error) in
                if !accept {
                    DispatchQueue.main.async {
                        self.notificationSwitch.isOn = false
                    }
                } else {
                    DispatchQueue.main.async {
                        self.notificationSwitch.isOn = true
                    }
                }
            }
        }
        
    
        @IBAction func switchNotification(_ sender: UISwitch) {
           
            if notificationSwitch.isOn == false {
                // 一个Alert提示框,用户点击确认之后再跳转到消息通知界面
                let alertController = UIAlertController(title: "温馨提示", message: "确认关闭消息通知?关闭消息通知将影响App的使用", preferredStyle: .alert)
                let cancelAction = UIAlertAction(title: "取消", style: .cancel) { (cancel) in
                    self.notificationSwitch.isOn = !self.notificationSwitch.isOn
                }
                
                let confirmAction = UIAlertAction(title: "确认", style: .default) { (okaction) in
                    // 跳转到消息通知界面
                    let url = URL(string: UIApplication.openSettingsURLString)
                    if let url = url, UIApplication.shared.canOpenURL(url) {
                        if #available(iOS 10, *) {
                            UIApplication.shared.open(url, options: [:], completionHandler: { (success) in
                            })
                        } else {
                            UIApplication.shared.openURL(url)
                        }
                    }
                    
                }
                alertController.addAction(cancelAction)
                alertController.addAction(confirmAction)
                self.present(alertController, animated: true, completion: nil)
            } else {
                 // 直接转到开启通知界面
                let url = URL(string: UIApplication.openSettingsURLString)
                if let url = url, UIApplication.shared.canOpenURL(url) {
                    if #available(iOS 10, *) {
                        UIApplication.shared.open(url, options: [:], completionHandler: { (success) in
                            
                        })
                    } else {
                        UIApplication.shared.openURL(url)
                    }
                }
            }
            
        }
    
    }
    
    //          ___
    //        //|||\\
    //       //|-_-|\\
    //          |||
    //           |
    //           |
    //          / \
    
    

    相关文章

      网友评论

          本文标题:[002][iOS-Swift]使用UISwitch控制本地消息

          本文链接:https://www.haomeiwen.com/subject/rinxqctx.html