Open Settings app from another app programmatically in iPhone

35,350

Solution 1

Good news :

You can open settings apps programmatically like this (works only from iOS8 onwards).

If you are using Swift 3.0:

UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!)

If you are using Objective-C:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];

For other lower versions (less than iOS8) its not possible to programatically open the settings app.

Solution 2

As others answered, you cannot open the Settings from your app.

However You can solve the situation, like I have done:

Output a message that Location services must be enabled explaining why, and show the path in that message:

"Settings->Privacy->LocationServices"

Solution 3

Opening settings apps programmatically is possible only from iOS 8. So, use the following code...

if([CLLocationManager locationServicesEnabled]&&
   [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied)
{
  //...Location service is enabled
}
else
{
    if([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0)
    {
       UIAlertView* curr1=[[UIAlertView alloc] initWithTitle:@"This app does not have access to Location service" message:@"You can enable access in Settings->Privacy->Location->Location Services" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
      [curr1 show];
    }
    else
    {
       UIAlertView* curr2=[[UIAlertView alloc] initWithTitle:@"This app does not have access to Location service" message:@"You can enable access in Settings->Privacy->Location->Location Services" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Settings", nil];
       curr2.tag=121;
       [curr2 show];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
   if (alertView.tag == 121 && buttonIndex == 1)
 {
  //code for opening settings app in iOS 8
   [[UIApplication sharedApplication] openURL:[NSURL  URLWithString:UIApplicationOpenSettingsURLString]];
 }
}

Solution 4

Till iOS 5.0 it was possible to open settings via the URL schema, i.e

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"My Settings URL"]];

This has been deprecated from iOS 5.1 onwards.

Solution 5

Here is a Swift2 version that worked for me including an Alert that instructs the user in what to do when the settings opens.

func initLocationManager() {
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()


// If there isn't a Lat/Lon then we need to see if we have access to location services
// We are going to ask for permission to use location if the user hasn't allowed it yet.
let status = CLLocationManager.authorizationStatus()
if(status == CLAuthorizationStatus.NotDetermined || status == CLAuthorizationStatus.Denied)  {

    //println(locationManager)

    //  check that locationManager is even avaiable.  If so, then ask permission to use it
    if locationManager != nil {
        locationManager.requestAlwaysAuthorization()

        //open the settings to allow the user to select if they want to allow for location settings.
        let alert = UIAlertController(title: "I Can't find you.", message: "To let my App figure out where you are on the map click 'Find Me' and change your location to 'Always' and come back to MyMobi.", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "No Thanks", style: UIAlertActionStyle.Default, handler:nil))
        alert.addAction(UIAlertAction(title: "Find Me", style: UIAlertActionStyle.Default, handler: {
            (alert: UIAlertAction!) in
            UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
        }))
        self.presentViewController(alert, animated: true, completion: nil)


    }
}
}
Share:
35,350
Edward Sagayaraj
Author by

Edward Sagayaraj

I am an iOS application developer

Updated on March 15, 2020

Comments

  • Edward Sagayaraj
    Edward Sagayaraj about 4 years

    I have to open settings app from my app if gps is not enabled in iPhone. I have used the following code. It works well in iOS simulator but it does not work in iPhone. May I know is there any problem in this code.

    if (![CLLocationManager locationServicesEnabled]) {
            int (*openApp)(CFStringRef, Boolean);
            void *hndl = dlopen("/System/Library/PrivateFrameworks/SpringBoardServices.framework/SpringBoardServices");
            openApp = (int(*)(CFStringRef, Boolean)) dlsym(hndl, "SBSLaunchApplicationWithIdentifier");
            openApp(CFSTR("com.apple.Preferences"), FALSE);
            dlclose(hndl);
        }
    
  • Oscar Gomez
    Oscar Gomez about 10 years
    This is still true unfortunately =(... no idea why this got depreacted on iOS 5.1
  • Dipesh KC
    Dipesh KC almost 10 years
    does this fallback gracefully for older versions?
  • nurnachman
    nurnachman almost 10 years
    Can it open a specific settings screen?
  • Yatheesha
    Yatheesha almost 10 years
    @nume currently its not possible.
  • William Denniss
    William Denniss over 9 years
    to make your code work on iOS 7, first check that UIApplicationOpenSettingsURLString exists like this: stackoverflow.com/a/25884389/72176
  • Tejas K
    Tejas K over 8 years
    @nurne - No, the above code will open your app's settings.
  • Krešimir Prcela
    Krešimir Prcela over 8 years
    It is possible to open any settings screen on any iOS 7,8,9. Check this answer: stackoverflow.com/a/34024467/475978
  • user3404693
    user3404693 over 7 years
    @YatheeshaBL no more working in iOS 10. Any workaround?
  • Khadija Daruwala
    Khadija Daruwala over 7 years
    @YatheeshaBL This solution is not working for iOS 10 . I am working on Xcode 8, swift 3. Any alternate solution?
  • Markus
    Markus over 6 years
    IS POSSIBLE TO ACCESS? Storage & iCloud Usage -> Manage Storage
  • funct7
    funct7 about 6 years
    options: and the trailing completionHandler: are unnecessary