swift 5.0 open settings location code example

Example 1: swift open settings page

guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else {
  return
}

if UIApplication.shared.canOpenURL(settingsUrl) {
  UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
                                                             print("Settings opened: \(success)") // Prints true
                                                            })
}

Example 2: how to get phone setting url in swfit 4

override func viewDidAppear(_ animated: Bool) {
    let alertController = UIAlertController (title: "Title", message: "Go to Settings?", preferredStyle: .alert)

    let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in

        guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else {
            return
        }

        if UIApplication.shared.canOpenURL(settingsUrl) {
            UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
                print("Settings opened: \(success)") // Prints true
            })
        }
    }
    alertController.addAction(settingsAction)
    let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
    alertController.addAction(cancelAction)

    present(alertController, animated: true, completion: nil)
}

Tags:

Misc Example