How to open your app in Settings iOS 11
Asked Answered
R

6

35

It seems that Apple has moved a lot of the app configurations to the App path with iOS 11, how to open the app path programmatically in Settings? I tried "App-Prefs:root=\(Bundle.main.bundleIdentifier!)" but this doesn't seem to work.

Please note that my question is specific to: How to open the app path in settings: NOT how to open the settings

Rite answered 26/9, 2017 at 8:41 Comment(0)
S
63

Here is the code you're looking for, I guess:

if let url = URL(string: UIApplicationOpenSettingsURLString) {
    if UIApplication.shared.canOpenURL(url) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    }
}

And in addition, the updated version for swift 5 :

if let url = URL(string: UIApplication.openSettingsURLString) {
    if UIApplication.shared.canOpenURL(url) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    }
}
Spoilage answered 26/9, 2017 at 9:39 Comment(5)
Note: UIApplication.shared.open(url, options: [:], completionHandler: nil) is iOS 10+.Weirdo
The call to canOpenURL is not needed - the subsequent call to open just fails (it doesn't raise an exception or crashes the app).Oren
Use UIApplication.shared.openURL(url) when targeting prior to IOS 10.Bouse
This opens your app in the Settings. Great!Curricle
if let url = URL.init(string: UIApplication.openSettingsURLString) { UIApplication.shared.open(url, options: [:], completionHandler: nil) }Creamcolored
B
16

Swift 4.2, iOS 12

Opening just the settings is possible with the function below:

extension UIApplication {

    ...

    @discardableResult
    static func openAppSettings() -> Bool {
        guard
            let settingsURL = URL(string: UIApplication.openSettingsURLString),
            UIApplication.shared.canOpenURL(settingsURL)
            else {
                return false
        }

        UIApplication.shared.open(settingsURL)
        return true
    }
}

Usage: UIApplication.openAppSettings()

But be careful to NOT use "non-public URL scheme", such as: prefs:root= or App-Prefs:root, because otherwise your app will be rejected. This happened to me recently since I was trying to have a deeplink to the wifi section in the settings.

Bandoleer answered 22/10, 2018 at 10:39 Comment(3)
You should probably name that UIApplication.openAppSettings() (notice the s) for compliance with openSettingsURLStringSaire
This take's you to app specific settings if they exist (like Siri usage, or contacts). IF they don't exist it takes you to the settings app and whatever page was last open. What it does not do, and what nobody has been able to figure out, is how to always open the Settings app, at the home screen, if your app has a settings page.Avigdor
Swift 4.2 UIApplicationOpenSettingsURLString was renamed to UIApplication.openSettingsURLStringFemineity
W
4

And if you want to make it work for both, older and newer iOS-versions, then do:

if let url = URL(string:UIApplicationOpenSettingsURLString) {
    if UIApplication.shared.canOpenURL(url) {
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(url)
        }
    }
}
Warp answered 21/8, 2018 at 10:12 Comment(0)
G
2

openURL has been deprecated since iOS 10, so I would advise you to use:

if let url = URL(string:UIApplicationOpenSettingsURLString) {
    if UIApplication.shared.canOpenURL(url) {
        UIApplication.shared.open(url, options: [:], completionHandler: { success in
            log.debug("Open app settings success: \(success)")
        })
    }
}
Goby answered 10/10, 2017 at 11:10 Comment(1)
Hi. I didnt find other string that open wifi setting screenDariusdarjeeling
U
1

UIApplicationOpenSettingsURLString has been renamed to UIApplication.openSettingsURLString.

if let url = URL(string: UIApplication.openSettingsURLString) {
    if UIApplication.shared.canOpenURL(url) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    }
}
Uuge answered 19/11, 2020 at 7:25 Comment(0)
S
0

SWift 5

In some case we can not open App's setting after trying all the above. To solve this problem 100% just make sure these two step are followed

Step 1. Right click on the project file -> Add New File -> Add Settings.Bundle in project and edit according to your requirements.

Step 2. Now add some code in your buttons action.

if let url = URL(string: UIApplication.openSettingsURLString) {
    if UIApplication.shared.canOpenURL(url) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    }
}

Note: Using "prefs:Root" is forbidden by apple and your app will be rejected. So, avoid using this api.

Stool answered 5/1, 2022 at 7:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.