Sol*_*ola 144 settings ios swift
我正在尝试在应用程序中实现一项功能,该功能在互联网连接不可用时显示警报.警报有两个操作(确定和设置),每当用户点击设置时,我想以编程方式将它们带到手机设置.
我正在使用Swift和Xcode.
Mar*_*anu 271
运用 UIApplication.openSettingsURLString
更新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: 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)
}
Run Code Online (Sandbox Code Playgroud)
Swift 2.x
运用 UIApplication.openSettingsURLString
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)
}
Run Code Online (Sandbox Code Playgroud)
viv*_*ani 221
斯威夫特4
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
Run Code Online (Sandbox Code Playgroud)
注意:以下方法适用于iOS 11以下的所有版本,对于更高版本的应用程序可能会被拒绝,因为它是私有API
有时我们希望将用户带到我们的应用设置以外的设置.以下方法将帮助您实现:
首先,在项目中配置URL Schemes.您可以在Target - > Info - > URL Scheme中找到它.单击+按钮并在URL Schemes中键入prefs
斯威夫特3
UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!, options: [:], completionHandler: nil)
Run Code Online (Sandbox Code Playgroud)
迅速
UIApplication.shared.open(URL(string:"App-Prefs:root=General")!, options: [:], completionHandler: nil)
Run Code Online (Sandbox Code Playgroud)
Objective-C的
UIApplication.sharedApplication().openURL(NSURL(string:"prefs:root=General")!)
Run Code Online (Sandbox Code Playgroud)
以下是所有可用的URL
注意:网络设置不会在模拟器中打开,但链接将在真实设备上运行.
Chr*_*örz 43
在iOS 8+中,您可以执行以下操作:
func buttonClicked(sender:UIButton)
{
UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString))
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特4
let settingsUrl = URL(string: UIApplicationOpenSettingsURLString)!
UIApplication.shared.open(settingsUrl)
Run Code Online (Sandbox Code Playgroud)
Luc*_*nzo 21
使用@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"
}
Run Code Online (Sandbox Code Playgroud)
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)
}
}
}
Run Code Online (Sandbox Code Playgroud)
这非常有用,因为API肯定会发生变化,你可以快速重构一次!
inc*_*don 16
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)
}
}
Run Code Online (Sandbox Code Playgroud)
在模拟器中的ios10/Xcode 8中:
UIApplication.shared.openURL(URL(string:UIApplicationOpenSettingsURLString)!)
Run Code Online (Sandbox Code Playgroud)
作品
UIApplication.shared.openURL(URL(string:"prefs:root=General")!)
Run Code Online (Sandbox Code Playgroud)
才不是.
我见过这行代码
UIApplication.sharedApplication() .openURL(NSURL(string:"prefs:root=General")!)
Run Code Online (Sandbox Code Playgroud)
不工作,它在ios10/Xcode 8中对我不起作用,只是一个小的代码差异,请替换它
UIApplication.sharedApplication().openURL(NSURL(string:"App-Prefs:root=General")!)
Run Code Online (Sandbox Code Playgroud)
Swift3
UIApplication.shared.openURL(URL(string:"prefs:root=General")!)
Run Code Online (Sandbox Code Playgroud)
用...来代替
UIApplication.shared.openURL(URL(string:"App-Prefs:root=General")!)
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你.干杯.
警告词:prefs:root或App-Prefs:rootURL方案被视为私有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:)方法已更新为包含非零选项字典,从该帖子开始只包含一个可能的类型选项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)
}
Run Code Online (Sandbox Code Playgroud)
明确构建一个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)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
103971 次 |
| 最近记录: |