【发布时间】:2015-03-25 00:11:30
【问题描述】:
我正在尝试在应用程序中实现一项功能,该功能会在互联网连接不可用时显示警报。 警报有两个操作(确定和设置),每当用户单击设置时,我想以编程方式将它们带到手机设置。
我正在使用 Swift 和 Xcode。
【问题讨论】:
我正在尝试在应用程序中实现一项功能,该功能会在互联网连接不可用时显示警报。 警报有两个操作(确定和设置),每当用户单击设置时,我想以编程方式将它们带到手机设置。
我正在使用 Swift 和 Xcode。
【问题讨论】:
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”)导致某些应用被商店拒绝。
【讨论】:
SWIFT 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)
【讨论】:
UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
URL(string: UIApplication.openSettingsURLString).map { UIApplication.shared.open($0, options: [:], completionHandler: nil) }
⚠️小心!
此答案基于未记录的 API,最近(自 iOS12 起)Apple 拒绝采用这种方法的应用程序。
原答案如下
斯威夫特 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 方案。您将在 Target -> Info -> URL Scheme 中找到它。单击 + 按钮并在 URL Schemes 中键入首选项
斯威夫特 5
UIApplication.shared.open(URL(string: "App-prefs:Bluetooth")!)
斯威夫特 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"]];
以下是所有可用的网址
**在IOS
在 IOS 13 上
应用偏好:DO_NOT_DISTURB
未测试
App-prefs:TWITTER (??)
注意:网络设置不会在模拟器中打开,但链接可以在真机上使用。
【讨论】:
App-Prefs 而不是 prefs 作为 URL 字符串的方案。例如App-Prefs:root=WIFI
App-Prefs 或 prefs:root,因为它们是私有 API,除非您非常幸运,否则您的应用会被拒绝。
使用UIApplication.openSettingsURLString
Swift 5.1 更新
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: UIApplication.openSettingsURLString) 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)
}
斯威夫特 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)
}
【讨论】:
Settings 和Cancel 按钮在viewDidAppear 方法上添加警报
UIApplication.sharedApplication().openURL(yourCustomURL),但您没有设置应用程序的访问权限
警告:prefs:root 或 App-Prefs:root URL 方案被视为私有 API。如果您使用这些应用程序,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。
【讨论】:
添加到@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)
}
}
【讨论】:
根据@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-Prefs:root=Privacy&path=LOCATION 为我提供了一般位置设置。注意:仅适用于设备。
【讨论】:
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)
}
}
【讨论】:
我看过这行代码
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")!)
希望对您有所帮助。 干杯。
【讨论】:
在 ios10/Xcode 8 模拟器中:
UIApplication.shared.openURL(URL(string:UIApplicationOpenSettingsURLString)!)
作品
UIApplication.shared.openURL(URL(string:"prefs:root=General")!)
没有。
【讨论】:
NS 前缀是否已被移除?