Answers:
使用 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)
}
viewDidAppear
,以使用Settings
和Cancel
按钮添加方法警报
UIApplication.sharedApplication().openURL(yourCustomURL)
从当前应用程序调用,但您没有从“设置”应用程序获得的访问权限
⚠️小心!
此答案基于未公开的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方案中键入首选项
斯威夫特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")!)
目标C
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=General"]];
以下是所有可用的网址
**在IOS <12上**
在IOS 13上
应用偏好:DO_NOT_DISTURB
未测试
应用偏好:TWITTER(??)
注意:网络设置不会在模拟器中打开,但是该链接将在真实设备上运行。
App-Prefs
代替prefs
URL字符串的方案。例如App-Prefs:root=WIFI
App-Prefs
或prefs:root
因为它们是私有API,除非您非常幸运,否则您的应用将被拒绝。
在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) }
根据@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肯定会发生变化,并且您可以一次且非常快速地进行重构!
在模拟器中的ios10 / Xcode 8中:
UIApplication.shared.openURL(URL(string:UIApplicationOpenSettingsURLString)!)
作品
UIApplication.shared.openURL(URL(string:"prefs:root=General")!)
才不是。
NS
iOS 10中是否已删除前缀?
我看过这行代码
UIApplication.sharedApplication() .openURL(NSURL(string:"prefs:root=General")!)
不起作用,在ios10 / Xcode 8中对我不起作用,只是很小的代码差异,请替换为
UIApplication.sharedApplication().openURL(NSURL(string:"App-Prefs:root=General")!)
迅捷3
UIApplication.shared.openURL(URL(string:"prefs:root=General")!)
用。。。来代替
UIApplication.shared.openURL(URL(string:"App-Prefs:root=General")!)
希望能帮助到你。干杯。
警告词: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.2,iOS 12
该open(url:options:completionHandler:)
方法已更新为包括一个非nil选项字典,截至本文发布时,该字典仅包含一个可能的类型选项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拒绝了商店中的某些应用。
添加到@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)
}
}
SWIFT 4
如果这正是您要查找的内容,则可以采用您应用程序的特定设置。
UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)