如何使用 swift 打开 iOS 中的定位服务页面?

How to open Location Services page in iOS using swift?

如果禁用,我需要要求用户启用定位服务。我创建了一个警报,询问用户是否要启用位置。我的用户单击“确定”,我想在 iPhone 上打开定位服务页面,如下所示:

我正在尝试 'openSettingsURLString' 但它打开的是应用程序设置而不是定位服务页面,这是代码:

            let url = URL(string: UIApplication.openSettingsURLString)

            let settingsAction = UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default) {
                (UIAlertAction) in UIApplication.shared.open(url!, options: [:], completionHandler: nil)
                self.dismiss(animated: true, completion: nil)
            }

            alertController.addAction(cancelAction)
            alertController.addAction(settingsAction)
            self.present(alertController, animated: true, completion: nil)

我相信这是不可能的,您只能导航到应用程序本身的设置; Apple 希望用户自己导航到定位服务。 .我建议向用户提供有关如何导航到定位服务的说明作为替代方案,例如一些屏幕截图和他们应该采取的路径。

⚠️ Caution

There is a way to achieve the following. However, as of iOS 12, Apple has begun rejecting apps that use App-Prefs or prefs:root as both methods are based on undocumented APIs.

打开设置应用程序 - 这个应该可以用。

UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)

打开定位服务

如果此应用仅供个人使用,并且您不打算将其提交到 App Store,那么您应该可以使用以下内容。否则,如果您确实打算发布您的应用程序,Apple 将在审核后拒绝它。

UIApplication.shared.open(URL(string: "App-prefs:LOCATION_SERVICES")!)