是否可以使用openURL打开设置应用程序?
我知道一个应用程序可以通过使用以下代码启动其他应用程序: [[UIApplication sharedApplication] openURL:appUrl];
。 而且我知道打开Safari浏览器和邮件的URLscheme,但是我做了一些search,没有发现settings.app的scheme。
您可以通过编程方式打开设置应用程序(仅适用于iOS8以上版本)。
如果你正在使用Swift:
UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString))
如果你正在使用Objective-C
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
对于其他较低版本(低于iOS8 ),不可能以编程方式打开设置应用程序。
你可以在iOS版本5.0-5.0.1中使用它。 它在iOS 5.1中被弃用了。
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs://"]];
以编程方式打开设置应用程序是可能的只有从iOS 8.所以,使用下面的代码从http://code-ios.blogspot.in/2014/10/opening-settings-app-from-another-app.html
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 { NSLog(@"buttonIndex:%d",buttonIndex); if (alertView.tag == 121 && buttonIndex == 1) { //code for opening settings app in iOS 8 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; } }
Swift 4版本:
if let url = URL(string: UIApplicationOpenSettingsURLString) { UIApplication.shared.openURL(url) }