是否可以使用openURL打开设置应用程序?

时间:2009-07-07 04:36:50

标签: iphone settings

我知道应用可以使用以下代码启动其他应用:[[UIApplication sharedApplication] openURL:appUrl];。我知道打开safari和邮件的URL方案,但我做了一些搜索,没有发现settings.app的方案。

4 个答案:

答案 0 :(得分:32)

您可以通过编程方式打开设置应用尝试此操作(仅适用于 iOS8 以上)。

如果您使用的是Swift:

    UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString))

如果您使用的是Objective-C

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

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

答案 1 :(得分:16)

您可以在iOS 5.0到5.0.1版本中使用它。然后在iOS 5.1中弃用了它。

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs://"]];

答案 2 :(得分:3)

只能从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]];
 }
}

答案 3 :(得分:1)

Swift 4 版本:

if let url = URL(string: UIApplicationOpenSettingsURLString) {
    UIApplication.shared.openURL(url)
}
相关问题