如何从设置屏幕打开位置服务屏幕?

How to open Location services screen from setting screen?

我想以编程方式打开位置服务屏幕以打开服务。

可以像下面的代码一样直接打开,

但首先在 Info.plist 的 URL 中设置 URL Schemes Type Like:

然后在特定事件的下方写下:

Objective - C :

[[UIApplication sharedApplication] openURL:
 [NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"]];

Swift中:

UIApplication.sharedApplication().openURL(NSURL(string: "prefs:root=LOCATION_SERVICES")!)

希望对您有所帮助。

第一个:

添加URL

转到项目设置 --> 信息 --> URL 类型 --> 添加新的 URL 方案

见下图:

其次:

使用以下代码打开位置设置:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"]];

引用自:

第 1 步:单击项目名称 >> 目标 >> 信息 >> url 类型

第 2 步:

-(IBAction)openSettingViewToEnableLocationService:(id)sender
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"]];
}

Swift 4.2

像这样直接进入您的应用设置:

if let bundleId = Bundle.main.bundleIdentifier,
    let url = URL(string: "\(UIApplication.openSettingsURLString)&path=LOCATION/\(bundleId)") 
{
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

将首选项添加为url类型后,使用以下代码直接进入应用程序的位置设置。

if let url = URL(string: "App-prefs:root=LOCATION_SERVICES") {
     UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

实际上有更简单的解决方案。它将显示您的应用程序设置以及位置 services/camera 访问权限等:

func showUserSettings() {
    guard let urlGeneral = URL(string: UIApplicationOpenSettingsURLString) else {
        return
    }
    UIApplication.shared.open(urlGeneral)
}

我已经尝试了以上所有答案,它在 iOS11 上不起作用。它只打开设置页面,而不是应用程序设置。最终这成功了。

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

Swift 4.2:

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

参考:https://developer.apple.com/documentation/uikit/uiapplicationopensettingsurlstring?language=swift

你想安全吗?使用 UIApplicationOpenSettingsURLString,这将打开应用程序设置,without deep-link

使用App-prefs你的应用将被拒绝,正如许多子评论所说。 https://github.com/mauron85/cordova-plugin-background-geolocation/issues/394

如果您设置了 locationManager.startUpdatingLocation() 并且您在 iphone 上禁用了它,它会自动向您显示一个 alertView,其中包含打开和激活位置的选项。

Swift 5+
Easy Way Direct 您所说的申请页面将会打开

if let BUNDLE_IDENTIFIER = Bundle.main.bundleIdentifier,
    let url = URL(string: "\(UIApplication.openSettingsURLString)&path=LOCATION/\(BUNDLE_IDENTIFIER)") {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}