web-dev-qa-db-fra.com

Ouvrez l'application Paramètres à partir d'une autre application par programmation sur iPhone

Je dois ouvrir l'application des paramètres depuis mon application si le GPS n'est pas activé sur l'iPhone. J'ai utilisé le code suivant. Cela fonctionne bien dans le simulateur iOS mais cela ne fonctionne pas dans l'iPhone. Puis-je savoir s'il y a un problème dans ce 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);
    }
35
Edward Sagayaraj

Bonnes nouvelles :

Vous pouvez ouvrir des applications de paramètres par programme comme ceci (ne fonctionne qu'à partir de iOS8).

Si vous utilisez Swift 3.0:

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

Si vous utilisez Objective-C:

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

Pour les autres versions inférieures (inférieures à iOS8), il n'est pas possible d'ouvrir par programme l'application des paramètres.

111
Yatheesha B L

Comme d'autres l'ont répondu, vous ne pouvez pas ouvrir les paramètres depuis votre application.

Cependant, vous pouvez résoudre la situation, comme je l'ai fait:

Générez un message indiquant que les services de localisation doivent être activés, expliquant pourquoi et affichez le chemin d'accès dans ce message:

"Paramètres-> Confidentialité-> LocationServices"

10
AlexWien

L'ouverture des applications de paramètres par programme n'est possible qu'à partir d'iOS 8. Donc, utilisez le code suivant ...

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]];
 }
}
6
Teja Kumar Bethina

Jusqu'à iOS 5.0, il était possible d'ouvrir settings via le URL schema, c'est à dire

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

Cela est obsolète à partir d'iOS 5.1.

3
footyapps27

Voici une version de Swift2 qui a fonctionné pour moi, y compris une alerte qui indique à l'utilisateur quoi faire lorsque les paramètres s'ouvrent.

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)


    }
}
}
3

openURL a été déconseillé dans iOS10.0: veuillez utiliser openURL: options: complétementHandler à la place

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