如果iPhone中没有启用gps,我必须从我的应用程序打开设置应用程序。我使用了以下代码。它在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);
}
答案 0 :(得分:111)
好消息:
您可以像这样以编程方式打开设置应用程序(仅适用于 iOS8 以上)。
如果您使用的是Swift 3.0:
UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!)
如果您使用的是Objective-C:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
对于其他较低版本(小于 iOS8 ),无法以编程方式打开设置应用。
答案 1 :(得分:10)
在其他人回答时,您无法从应用中打开“设置”。
但是你可以解决这个问题,就像我做的那样:
输出一条消息,必须启用位置服务来解释原因,并在该消息中显示路径:
“设置 - >与隐私> LocationServices”
答案 2 :(得分:6)
只能从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]];
}
}
答案 3 :(得分:3)
直到iOS 5.0,可以通过settings
打开URL schema
,即
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"My Settings URL"]];
从iOS 5.1起,这已被弃用。
答案 4 :(得分:3)
这是一个适合我的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)
}
}
}
答案 5 :(得分:3)
在iOS10.0中不推荐使用openURL:请使用openURL:options:completionHandler而不是
let url = URL(string: UIApplicationOpenSettingsURLString)!
UIApplication.shared.open(url, options: [:]) { success in }