How To Open Settings In iOS Programmatically

By Eddy Chung

An iOS application I was developing required the user to change some of their settings.

I wondered how to open the settings app smoothly from my own application to make the process as smooth as possible. Here’s how I figured out how to do it.

How to open settings in iOS programmatically? Create a URL using the openSettingsURLString type property and then open it using the openURL method.

Depending on the version of iOS you are targeting, there can be different methods. Some methods allow you to open a specific page in settings. However, be careful, Apple marks some of these as private APIs and this can lead to your app being rejected.

How To Open Settings In iOS With Code Examples

To open up the settings in Swift code, first get the URL from the openSettingsURLString property that Apple provides. Then open that URL using the openURL method.

UIApplication.shared.open(URL.init(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)

Now to do it safely without the force unwrap (!) you could do something like this:

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

To put it on an alert dialog with two options, open settings or dismiss, you could follow this code example.

func openSettings(alert: UIAlertAction!) {
    if let url = URL.init(string: UIApplication.openSettingsURLString) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    }
}

let alert = UIAlertController(title: "Settings",
                              message: "Please modify your settings",
                              preferredStyle: .alert)

alert.addAction(UIAlertAction(title: "Open Settings",
                              style: UIAlertAction.Style.default,
                              handler: openSettings))
alert.addAction(UIAlertAction(title: "Cancel",
                              style: UIAlertAction.Style.default,
                              handler: nil))

viewController.present(alert, animated: true, completion: nil)

How To Open Specific Settings Page in iOS

These methods are considered private APIs by Apple and their use may get your application rejected from the App Store. Repeated use of private APIs may cause your Apple Developer account to be suspended. These methods can deep link to specific pages in the Settings app (such as Wifi, Bluetooth, etc).

zero to app store

For this reason, we do not recommend using any links such as prefs:root= as you will receive the above rejection notice from Apple.

For reference, here is a list of the private APIs (subject to changes by Apple):

prefs:root=General&path=About
prefs:root=General&path=ACCESSIBILITY
prefs:root=AIRPLANE_MODE
prefs:root=General&path=AUTOLOCK
prefs:root=General&path=USAGE/CELLULAR_USAGE
prefs:root=Brightness
prefs:root=General&path=Bluetooth
prefs:root=General&path=DATE_AND_TIME
prefs:root=FACETIME
prefs:root=General
prefs:root=General&path=Keyboard
prefs:root=General&path=INTERNATIONAL
prefs:root=LOCATION_SERVICES
prefs:root=ACCOUNT_SETTINGS
prefs:root=MUSIC
prefs:root=General&path=Network
prefs:root=NIKE_PLUS_IPOD
prefs:root=NOTES
prefs:root=NOTIFICATIONS_ID
prefs:root=Phone
prefs:root=Photos
prefs:root=Sounds&path=Ringtone
prefs:root=Safari
prefs:root=General&path=Assistant
prefs:root=Sounds
prefs:root=General&path=USAGE
prefs:root=VIDEO
prefs:root=General&path=Network/VPN
prefs:root=Wallpaper
prefs:root=WIFI
prefs:root=INTERNET_TETHERING

Permissions in iOS

You can request for specific permissions from your application without opening the settings app. These permissions can range from accessing location, calendar, contacts and a lot more.

You should aim to get permission the first time around

When a user clicks Don’t Allow there is no easy way for them to reverse that decision. The user has to go manually into the settings to allow a certain permission to the application.

This could break your user experience entirely, if your application depends on these permissions such as location or access to photos.

Only ask for permissions when you are certain you’ll get access. That is why it is bad to ask for a bunch of permissions on app launch, as this is when the user is most skeptical. The user just installed your app, so begin to build that trust before asking for permission.

Context is important

Users are more likely to allow permissions when they’re asked contextually. There has to be a clear feature in your app that uses this permission and it must be obvious to the user.

Out of context asks for permissions are more likely to be rejected.

Make it based off a user action

The best way to get permissions from the user is to make it based off a user action. For example, if the user clicks a button that says Find Nearby … then you ask for location permissions, it is highly likely the user will grant permissions, since it was based off their own action.

The same can applied to other actions such as Take Photo Button followed by asking for photo permissions.

This is the best way to ask for permissions and has the highest chances of it being granted.

When should I ask users for permissions or settings change for my mobile app? You should try to keep this contextual as possible. It is best to ask for the permission right before you need it. That way the user knows why the app needs the permission or settings change

What happens if a user accidentally denies permission? The user will have to go into their settings to enable a specific permission for you app. You cannot display another alert dialog to grant permission.

If you liked this post, you'll love my free guide: Secrets To iOS Development. Speed up your learning curve - hundreds of students have already downloaded. Thanks for reading!
Profile Picture of Eddy Chung

Eddy Chung

I am a professional iOS developer in Silicon Valley. I teach iOS development on ZeroToAppStore.com. If you'd like to learn more about me click here or you can contact me at: eddy@zerotoappstore.com

Similar Posts