Edward Sagayaraj
Edward Sagayaraj

Reputation: 471

Open Settings app from another app programmatically in iPhone

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);
    }

Upvotes: 36

Views: 35583

Answers (6)

Yatheesha
Yatheesha

Reputation: 10432

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.

Upvotes: 112

Aravindhan
Aravindhan

Reputation: 15628

openURL was deprecated in iOS10.0: Please use openURL:options:completionHandler instead

let url = URL(string: UIApplicationOpenSettingsURLString)!
UIApplication.shared.open(url, options: [:]) { success in }

Upvotes: 3

Christopher Wade Cantley
Christopher Wade Cantley

Reputation: 7232

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)


    }
}
}

Upvotes: 3

Teja Kumar Bethina
Teja Kumar Bethina

Reputation: 3706

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]];
 }
}

Upvotes: 6

AlexWien
AlexWien

Reputation: 28727

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"

Upvotes: 10

footyapps27
footyapps27

Reputation: 4042

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.

Upvotes: 3

Related Questions