Tuesday, January 13, 2015

Open the Settings app Programmatically from Xamarin iOS

 

When Apple released iOS 8, it came with a massive API update.  They released a number of new frameworks and over 4000 new APIs.  So needless to say, I’m still discovering new toys and tricks that released with the latest version.

A really cool feature included in iOS 8 is the ability to launch the built in settings app from within our applications; something we haven’t been able to do since iOS 5 (and was removed again in iOS 5.1).

This is useful for multiple reasons.  First, you can add app preferences and expose them through the settings app: 

https://github.com/xamarin/monotouch-samples/tree/master/AppPrefs 

However, this isn’t all that common, primarily because it required the user to leave your app and search through the Settings app.

But it also means we have quick way to push users to toggle things like permission to use location data.

Fortunately it’s also fairly easy to do, we simply need to create and open a NSUrl with the correct string: {app-settings:}.  And this is conveniently available as a static property on the UIApplication class.

Do note, this only works in iOS8 so if you’re app is compatible with older versions of iOS, you should check you’re running on iOS8 first; this will cause an exception on older versions.

if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
var settingsString
= UIKit.UIApplication.OpenSettingsUrlString;
var url
= new NSUrl (settingsString);
UIApplication.SharedApplication.OpenUrl (url);
}


OpenSettings