Opening the Settings app from another app

前端 未结 17 2190
傲寒
傲寒 2020-11-22 01:37

Okay, I know that there are many question about it, but they are all from many time ago.

So. I know that it is possible because the Map app does it.

In the M

17条回答
  •  灰色年华
    2020-11-22 01:59

    Add this to your class,

     public class func showSettingsAlert(title:String,message:String,onVC viewController:UIViewController,onCancel:(()->())?){
                YourClass.show2ButtonsAlert(onVC: viewController, title: title, message: message, button1Title: "Settings", button2Title: "Cancel", onButton1Click: {
                    if let settingsURL = NSURL(string: UIApplicationOpenSettingsURLString){
                        UIApplication.sharedApplication().openURL(settingsURL)
                    }
                    }, onButton2Click: {
                        onCancel?()
                })
            }
    
     public class func show2ButtonsAlert(onVC viewController:UIViewController,title:String,message:String,button1Title:String,button2Title:String,onButton1Click:(()->())?,onButton2Click:(()->())?){
                dispatch_async(dispatch_get_main_queue()) {
                    let alert : UIAlertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
    
                    alert.addAction(UIAlertAction(title: button1Title, style:.Default, handler: { (action:UIAlertAction) in
                        onButton1Click?()
                    }))
    
                    alert.addAction(UIAlertAction(title: button2Title, style:.Default, handler: { (action:UIAlertAction) in
                        onButton2Click?()
                    }))
    
                    viewController.presentViewController(alert, animated: true, completion: nil)
                }
            }
    

    Call like this,

    YourClass.showSettingsAlert("App would like to access camera", message: "App would like to access camera desc", onVC: fromViewController, onCancel: {
      print("canceled")
    })
    

提交回复
热议问题