我正在尝试在应用程序中实现一项功能,该功能在互联网连接不可用时显示警报。警报有两个操作(确定和设置),每当用户点击设置时,我想以编程方式将它们带到手机设置。
我正在使用Swift和Xcode。
使用UIApplicationOpenSettingsURLString
更新Swift 4.2
override func viewDidAppear(_ animated: Bool) {
let alertController = UIAlertController (title: "Title", message: "Go to Settings?", preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
print("Settings opened: \(success)") // Prints true
})
}
}
alertController.addAction(settingsAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
}
Swift 2.x
使用UIApplicationOpenSettingsURLString
override func viewDidAppear(animated: Bool) {
var alertController = UIAlertController (title: "Title", message: "Go to Settings?", preferredStyle: .Alert)
var settingsAction = UIAlertAction(title: "Settings", style: .Default) { (_) -> Void in
let settingsUrl = NSURL(string: UIApplicationOpenSettingsURLString)
if let url = settingsUrl {
UIApplication.sharedApplication().openURL(url)
}
}
var cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: nil)
alertController.addAction(settingsAction)
alertController.addAction(cancelAction)
presentViewController(alertController, animated: true, completion: nil)
}
添加到@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)
}
}
警告:prefs:root
或App-Prefs:root
URL方案被视为私有API。如果您使用Apple,Apple可能拒绝您的应用程序,这是您在提交应用程序时可能会得到的内容:
您的应用使用“prefs:root =”非公共URL方案,这是一个私有实体。 App Store上不允许使用非公共API,因为如果这些API发生变化,可能会导致糟糕的用户体验。在将来提交此应用程序时继续使用或隐藏非公共API可能会导致Apple Developer帐户被终止,以及从App Store中删除所有相关应用程序。
下一步
要解决此问题,请修改您的应用以使用公共API提供相关功能,或使用“prefs:root”或“App-Prefs:root”URL方案删除功能。
如果没有其他方法可以提供您的应用所需的功能,则可以提交增强请求。
SWIFT 4
这可以采取您的应用程序的具体设置,如果这是您正在寻找的。
UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
如上所述@niravdesai说App-prefs。我发现App-Prefs:
适用于iOS 9,10和11设备测试。 prefs:
仅适用于iOS 9。
斯威夫特5
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
斯威夫特4
UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!, options: [:], completionHandler: nil)
注意:以下方法适用于iOS 11以下的所有版本,对于更高版本的应用程序可能会被拒绝,因为它是私有API
有时我们希望将用户带到我们的应用设置以外的设置。以下方法将帮助您实现:
首先,在项目中配置URL Schemes。您可以在Target - > Info - > URL Scheme中找到它。单击+按钮并在URL Schemes中键入prefs
斯威夫特3
UIApplication.shared.open(URL(string:"App-Prefs:root=General")!, options: [:], completionHandler: nil)
迅速
UIApplication.sharedApplication().openURL(NSURL(string:"prefs:root=General")!)
Objective-C的
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=General"]];
以下是所有可用的URL
注意:网络设置不会在模拟器中打开,但链接将在真实设备上运行。
在iOS 8+中,您可以执行以下操作:
func buttonClicked(sender:UIButton)
{
UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString))
}
斯威夫特4
let settingsUrl = URL(string: UIApplicationOpenSettingsURLString)!
UIApplication.shared.open(settingsUrl)
使用@vivek的提示我开发了一个基于Swift 3的utils类,希望你欣赏!
import Foundation
import UIKit
public enum PreferenceType: String {
case about = "General&path=About"
case accessibility = "General&path=ACCESSIBILITY"
case airplaneMode = "AIRPLANE_MODE"
case autolock = "General&path=AUTOLOCK"
case cellularUsage = "General&path=USAGE/CELLULAR_USAGE"
case brightness = "Brightness"
case bluetooth = "Bluetooth"
case dateAndTime = "General&path=DATE_AND_TIME"
case facetime = "FACETIME"
case general = "General"
case keyboard = "General&path=Keyboard"
case castle = "CASTLE"
case storageAndBackup = "CASTLE&path=STORAGE_AND_BACKUP"
case international = "General&path=INTERNATIONAL"
case locationServices = "LOCATION_SERVICES"
case accountSettings = "ACCOUNT_SETTINGS"
case music = "MUSIC"
case equalizer = "MUSIC&path=EQ"
case volumeLimit = "MUSIC&path=VolumeLimit"
case network = "General&path=Network"
case nikePlusIPod = "NIKE_PLUS_IPOD"
case notes = "NOTES"
case notificationsId = "NOTIFICATIONS_ID"
case phone = "Phone"
case photos = "Photos"
case managedConfigurationList = "General&path=ManagedConfigurationList"
case reset = "General&path=Reset"
case ringtone = "Sounds&path=Ringtone"
case safari = "Safari"
case assistant = "General&path=Assistant"
case sounds = "Sounds"
case softwareUpdateLink = "General&path=SOFTWARE_UPDATE_LINK"
case store = "STORE"
case twitter = "TWITTER"
case facebook = "FACEBOOK"
case usage = "General&path=USAGE"
case video = "VIDEO"
case vpn = "General&path=Network/VPN"
case wallpaper = "Wallpaper"
case wifi = "WIFI"
case tethering = "INTERNET_TETHERING"
case blocked = "Phone&path=Blocked"
case doNotDisturb = "DO_NOT_DISTURB"
}
enum PreferenceExplorerError: Error {
case notFound(String)
}
open class PreferencesExplorer {
// MARK: - Class properties -
static private let preferencePath = "App-Prefs:root"
// MARK: - Class methods -
static func open(_ preferenceType: PreferenceType) throws {
let appPath = "\(PreferencesExplorer.preferencePath)=\(preferenceType.rawValue)"
if let url = URL(string: appPath) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
} else {
throw PreferenceExplorerError.notFound(appPath)
}
}
}
这非常有用,因为API肯定会发生变化,你可以快速重构一次!
App-Specific URL Schemes的第一个回复在iOS 10.3上为我工作。
if let appSettings = URL(string: UIApplicationOpenSettingsURLString + Bundle.main.bundleIdentifier!) {
if UIApplication.shared.canOpenURL(appSettings) {
UIApplication.shared.open(appSettings)
}
}
App-Prefs:root=Privacy&path=LOCATION
为我进入一般位置设置工作。注意:仅适用于设备。
在模拟器中的ios10 / Xcode 8中:
UIApplication.shared.openURL(URL(string:UIApplicationOpenSettingsURLString)!)
作品
UIApplication.shared.openURL(URL(string:"prefs:root=General")!)
才不是。
我见过这行代码
UIApplication.sharedApplication() .openURL(NSURL(string:"prefs:root=General")!)
不工作,它在ios10 / Xcode 8中对我不起作用,只是一个小的代码差异,请替换它
UIApplication.sharedApplication().openURL(NSURL(string:"App-Prefs:root=General")!)
Swift3
UIApplication.shared.openURL(URL(string:"prefs:root=General")!)
用。。。来代替
UIApplication.shared.openURL(URL(string:"App-Prefs:root=General")!)
希望能帮助到你。干杯。
Swift 4.2,iOS 12
open(url:options:completionHandler:)
方法已更新为包含非零选项字典,该帖子仅包含一个类型为UIApplication.OpenExternalURLOptionsKey
的可能选项(在示例中)。
@objc func openAppSpecificSettings() {
guard let url = URL(string: UIApplication.openSettingsURLString),
UIApplication.shared.canOpenURL(url) else {
return
}
let optionsKeyDictionary = [UIApplication.OpenExternalURLOptionsKey(rawValue: "universalLinksOnly"): NSNumber(value: true)]
UIApplication.shared.open(url, options: optionsKeyDictionary, completionHandler: nil)
}
明确构建一个URL,例如“App-Prefs”,AFAIK已经从商店中删除了一些应用程序。