在iPhone中以编程方式从另一个应用打开设置应用

如果gps在iPhone中未启用,我必须从我的应用程序打开设置应用程序。 我已经使用了下面的代码。 它在iOS模拟器中运行良好,但在iPhone中不起作用。 请问这个代码有没有问题?

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

好消息 :

您可以像这样以编程方式打开设置应用程序(仅适用于iOS8以上)。

如果你正在使用Swift 3.0:

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

如果您正在使用Objective-C:

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

对于其他较低版本(小于iOS8 ),不可能以编程方式打开设置应用程序。

正如其他人回答,你不能从你的应用程序打开设置。

但是你可以像我所做的那样解决这个问题:

输出必须启用位置服务的消息,解释原因,并显示该消息中的path:

“设置 – >与隐私> LocationServices”

以编程方式打开设置应用程序是可能的只有从iOS 8.所以,使用下面的代码…

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

在iOS 5.0中,可以通过URL schema打开settings ,即

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

这从iOS 5.1开始已经被弃用了。

这里是一个Swift2版本,包括一个警报,指导用户在设置打开时该怎么做。

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

openURL在iOS10.0中已被弃用:请改用openURL:options:completionHandler

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