我试图在一个应用程序中实现一个功能,当互联网连接不可用时显示警报。 警报有两个动作(确定和设置),每当用户单击设置,我想以编程方式将他们带到电话设置。
我使用Swift和Xcode。
我试图在一个应用程序中实现一个功能,当互联网连接不可用时显示警报。 警报有两个动作(确定和设置),每当用户单击设置,我想以编程方式将他们带到电话设置。
我使用Swift和Xcode。
当前回答
如上所述@niravdesai说App-prefs。 我发现App-Prefs:适用于iOS 9、10和11。设备测试。 where as prefs:仅适用于iOS 9。
其他回答
斯威夫特5
if let settingsUrl = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(settingsUrl)
}
在iOS 8+中,您可以执行以下操作:
func buttonClicked(sender:UIButton)
{
UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString))
}
斯威夫特4
let settingsUrl = URL(string: UIApplicationOpenSettingsURLString)!
UIApplication.shared.open(settingsUrl)
如上所述@niravdesai说App-prefs。 我发现App-Prefs:适用于iOS 9、10和11。设备测试。 where as prefs:仅适用于iOS 9。
在ios10/ Xcode 8模拟器:
UIApplication.shared.openURL(URL(string:UIApplicationOpenSettingsURLString)!)
作品
UIApplication.shared.openURL(URL(string:"prefs:root=General")!)
没有。
UIApplication.open(_:options:completionHandler:)只能在主线程中使用
解决方案:
if let appSettings = URL(string: UIApplication.openSettingsURLString + Bundle.main.bundleIdentifier!) {
if UIApplication.shared.canOpenURL(appSettings) {
DispatchQueue.main.async {
UIApplication.shared.open(appSettings)
}
}
}
@Luca Davanzo
iOS 11,一些权限设置已经移动到应用程序路径:
iOS 11支持
static func open(_ preferenceType: PreferenceType) throws {
var preferencePath: String
if #available(iOS 11.0, *), preferenceType == .video || preferenceType == .locationServices || preferenceType == .photos {
preferencePath = UIApplicationOpenSettingsURLString
} else {
preferencePath = "\(PreferencesExplorer.preferencePath)=\(preferenceType.rawValue)"
}
if let url = URL(string: preferencePath) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
} else {
throw PreferenceExplorerError.notFound(preferencePath)
}
}