'How to open your app's settings (inside the Settings app) with Swift (iOS 11)?
I would like to open my app's settings page inside the Settings app with Swift 4 in iOS 11. Just like the picture shows:
The following codes doesn't work, it will only open the Settings app:
if let url = URL(string:UIApplicationOpenSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
The app shows in the above picture is able to do this. So I wonder if there is any way to custom the URL Scheme to make it happen?
Solution 1:[1]
Oops, it seems it works in iOS 11.4.1:
if let url = URL(string:UIApplicationOpenSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
Solution 2:[2]
Just an update because UIApplicationOpenSettingsURLString changed.
guard let url = URL(string: UIApplication.openSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:])
}
Solution 3:[3]
You can open your app settings screen using it's bundle id, for example for CAMERA permission, you can use
if let bundleId = /* your app's bundle identifier */
let url = URL(string: "\(UIApplication.openSettingsURLString)&path=CAMERA/\(bundleId)"),
UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
Reference: https://stackoverflow.com/a/61383270/4439983
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Vincent |
Solution 2 | Joule87 |
Solution 3 | Noor |