如何以编程方式打开iOS 11+中的“设置>隐私>位置服务”?

12
如果 locationServicesEnabled 返回 false,我会提示用户启用他们的位置服务。以下URL适用于10.0+,将用户重定向到设置应用程序并直接转到“位置服务”屏幕:
URL(string: "App-Prefs:root=Privacy&path=LOCATION")
然而,在iOS 11中,这种方法不起作用。它会打开设置应用程序,但无法浏览到“位置服务”。有人知道 iOS 11+ 的新 URL 是什么吗?
3个回答

5

苹果在这个链接上发布了一份明确允许的URL列表。不幸的是,如果您使用其他URL(例如您正在尝试使用的URL),这可能会导致您的应用被App Store拒绝。

简短回答:如果不违反App Store规定,您无法实现您想要做的事情。


3
在 iOS 15 中,这对我来说无效,因为它只是打开了通用设置页面,但基于 shivi_shub 的答案和他的 comment,我尝试了以下方法:
if let bundleId = Bundle.main.bundleIdentifier,
   let url = URL(string: "\(UIApplication.openSettingsURLString)&path=LOCATION_SERVICES/\(bundleId)")
{
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

这将带我到位置设置页面,至少更接近于我真正想展示的设置页面。

所以我猜我们可以这样做:

if let url = URL(string: "\(UIApplication.openSettingsURLString)&path=LOCATION_SERVICES") {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

在iOS 16中,LOCATION_SERVICES无法正常工作。 - kelin

2
使用这个 -
UIApplication.shared.open(URL(string:UIApplicationOpenSettingsURLString)!)

使用App-prefs可能导致应用在应用商店上被拒绝。
或者您也可以尝试这个-
if let bundleId = Bundle.main.bundleIdentifier,
let url = URL(string: "\(UIApplication.openSettingsURLString)&path=LOCATION/\(bundleId)") {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

但是我想打开位置的常规设置选项卡(在那里它被停用),而不是我的应用程序的设置选项卡 :( - zeus
尝试将URL更改为-- URL(string: "App-prefs:root=LOCATION_SERVICES") - shivi_shub
不支持iOS 11 :( 你解决了吗? - zeus
2
if let url = URL(string:UIApplicationOpenSettingsURLString) { UIApplication.shared.open(url) } - SwiftArchitect
1
SwiftArchitect提出的解决方案部分有效 - 它成功地打开了系统设置应用程序,但OP问题特别是如何打开设置的子部分,即位置、常规或隐私。以前的URL扩展,如“root=General”,在iOS 12中不起作用。 - BlueskyMed

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接