Adam Diament
Adam Diament

Reputation: 4840

How to open settings programmatically in ios

I was searching for the Xamarin implementation of How to open settings programmatically

Vito-ziv answered it for objective C - what is the correct way to do this in C# for iOS in Xamarin Studio?

Upvotes: 2

Views: 4561

Answers (2)

JKLTechnologies
JKLTechnologies

Reputation: 31

Hope this will help you. This is working in iPhone not sure about working on iPad.

var url = new NSUrl("prefs:root=Settings");
UIApplication.SharedApplication.OpenUrl(url);

Upvotes: 1

Adam Diament
Adam Diament

Reputation: 4840

For current devices this is only possible in ios8 (ios9 not available at time of writing) (It used to be possible before ios5 apparently - see this blog post from Adrian Stevens at Xamarin - shout out to him for the inspiration for this answer)

To do it in ios8, I did it like this:

var settingsString = UIKit.UIApplication.OpenSettingsUrlString;
            var url = new NSUrl (settingsString);
            UIApplication.SharedApplication.OpenUrl (url);

Where the above code was called from a click event via delegate class in a UIAlertView click.

Since I am supporting ios7 too, to handle ios7 devices I did this, where the HandleLocationAuthorisation method is called when deciding whether to present a view controller - the user on ios8 and above can choose to go to the settings directly, whereas the user on ios7 has to go there manually.

This example below is checking for location services, but with trivial changes could easily be changed to check for other types of settings.

public bool HandleLocationAuthorisation () 
{

    if (CLLocationManager.Status == CLAuthorizationStatus.AuthorizedAlways) {
        return true;
    } else {
        UIAlertView uiAlert;  


        //iOS 8 and above can redirect to settings from within the app
        if (UIDevice.CurrentDevice.CheckSystemVersion(8,0)) {
            uiAlert = new UIAlertView 
                ("Location Services Required", 
                    "",
                    null,
                    "Return To App","Open Settings");

            uiAlert.Delegate = new OpenSettingsFromUiAlertViewDelegate();
            uiAlert.Message = "Authorisation to use your location is required to use this feature of the app.";

        //ios7 and below has to go there manually
        } else {
            uiAlert = new UIAlertView 
                ("Location Services Required", 
                    "Authorisation to use your location is required to use this feature of the app. To use this feature please go to the settings app and enable location services",
                    null,
                    "Ok");
        }
        uiAlert.Show ();
        return false;
    }

}

For completeness, here is the code for the event delgate referenced above:

public class OpenSettingsFromUiAlertViewDelegate : UIAlertViewDelegate {

public override void Clicked (UIAlertView alertview, nint buttonIndex)
{

    if (buttonIndex == 1) {
        var settingsString = UIKit.UIApplication.OpenSettingsUrlString;
        var url = new NSUrl (settingsString);
        UIApplication.SharedApplication.OpenUrl (url);
    }
  }
}

Upvotes: 3

Related Questions