Open Settings app from another app programmatically in iPhone

前端 未结 6 900
陌清茗
陌清茗 2020-12-07 18:55

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

相关标签:
6条回答
  • 2020-12-07 19:26

    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)
    
    
        }
    }
    }
    
    0 讨论(0)
  • 2020-12-07 19:34

    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.

    0 讨论(0)
  • 2020-12-07 19:42

    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.

    0 讨论(0)
  • 2020-12-07 19:42

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

    let url = URL(string: UIApplicationOpenSettingsURLString)!
    UIApplication.shared.open(url, options: [:]) { success in }
    
    0 讨论(0)
  • 2020-12-07 19:46

    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"

    0 讨论(0)
  • 2020-12-07 19:52

    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]];
     }
    }
    
    0 讨论(0)
提交回复
热议问题