Linking to iOS Settings

iOS Apps use settings as one way to hold different values that can be used to configure the use of an application. The Settings are provided centrally and are accessed through the Settings app from Apple.

As of iOS 8, there is a way to open the Settings app linked directly to your App's settings information. Using the openURL method on a UIApplication, you can pass in the string UIApplicationOpenSettingsURLString.

UIApplication.sharedApplication().openURL(NSURL(string: 
   UIApplicationOpenSettingsURLString)!)

The openURL method call could take time to process, which would cause the user interface to freeze. Therefore, it is typical to use a Grand Central Dispatch (GCD) function to defer the scheduling back on to the main thread, thus avoiding the UI freeze.

An example is shown below.

dispatch_async(dispatch_get_main_queue(), {
        UIApplication.sharedApplication().openURL(
            NSURL(string: 
                UIApplicationOpenSettingsURLString)!)
})

So, for example, if you wanted to open the Settings App in a handler for a UIAlertAction, then you can use the above code.

let settingsAction = 
    UIAlertAction(title: "Settings", style: .Default, 
        handler: { 
            (UIAlertAction) in
            dispatch_async(dispatch_get_main_queue(), {
               UIApplication.sharedApplication().openURL(
                  NSURL(string: UIApplicationOpenSettingsURLString)!)
            })
        })

The above code would call the closure if the UIAlertAction is selected by the user. This would place the call to openURL and the handler would exit, The call to openURL would be processed in due course.