我试图在一个应用程序中实现一个功能,当互联网连接不可用时显示警报。 警报有两个动作(确定和设置),每当用户单击设置,我想以编程方式将他们带到电话设置。

我使用Swift和Xcode。


使用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)
}

斯威夫特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)

⚠️小心! 这个答案是基于未记录的api,最近(从iOS12开始)苹果拒绝使用这种方法的应用程序。

以下是原始答案

斯威夫特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方案。你会发现它在目标->信息-> URL方案。点击+按钮,在URL方案中输入prefs

斯威夫特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"]];

和下面是所有可用的url

**在IOS上< 12 **

prefs:root=General&path=About prefs:root=General&path=ACCESSIBILITY prefs:root=AIRPLANE_MODE prefs:root=General&path=AUTOLOCK prefs:root=General&path=USAGE/CELLULAR_USAGE prefs:root=Brightness prefs:root=Bluetooth prefs:root=General&path=DATE_AND_TIME prefs:root=FACETIME prefs:root=General prefs:root=General&path=Keyboard prefs:root=CASTLE prefs:root=CASTLE&path=STORAGE_AND_BACKUP prefs:root=General&path=INTERNATIONAL prefs:root=LOCATION_SERVICES prefs:root=ACCOUNT_SETTINGS prefs:root=MUSIC prefs:root=MUSIC&path=EQ prefs:root=MUSIC&path=VolumeLimit prefs:root=General&path=Network prefs:root=NIKE_PLUS_IPOD prefs:root=NOTES prefs:root=NOTIFICATIONS_ID prefs:root=Phone prefs:root=Photos prefs:root=General&path=ManagedConfigurationList prefs:root=General&path=Reset prefs:root=Sounds&path=Ringtone prefs:root=Safari prefs:root=General&path=Assistant prefs:root=Sounds prefs:root=General&path=SOFTWARE_UPDATE_LINK prefs:root=STORE prefs:root=TWITTER prefs:root=FACEBOOK prefs:root=General&path=USAGE prefs:root=VIDEO prefs:root=General&path=Network/VPN prefs:root=Wallpaper prefs:root=WIFI prefs:root=INTERNET_TETHERING prefs:root=Phone&path=Blocked prefs:root=DO_NOT_DISTURB

在IOS 13上

App-prefs:General&path=About App-prefs:AIRPLANE_MODE App-prefs:General&path=AUTOLOCK App-prefs:Bluetooth App-prefs:General&path=DATE_AND_TIME App-prefs:FACETIME App-prefs:General App-prefs:General&path=Keyboard App-prefs:CASTLE App-prefs:CASTLE&path=STORAGE_AND_BACKUP App-prefs:General&path=INTERNATIONAL App-prefs:MUSIC App-prefs:NOTES App-prefs:NOTIFICATIONS_ID App-prefs:Phone App-prefs:Photos App-prefs:General&path=ManagedConfigurationList App-prefs:General&path=Reset App-prefs:Sounds&path=Ringtone App-prefs:Sounds App-prefs:General&path=SOFTWARE_UPDATE_LINK App-prefs:STORE App-prefs:Wallpaper App-prefs:WIFI App-prefs:INTERNET_TETHERING App-prefs:DO_NOT_DISTURB Not tested App-prefs:TWITTER (??) App-prefs:FACEBOOK (??) App-prefs:NIKE_PLUS_IPOD (??)

注意:网络设置将不会在模拟器中打开,但该链接将在真实设备上工作。


在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")!)

希望能有所帮助。 欢呼。


如上所述@niravdesai说App-prefs。 我发现App-Prefs:适用于iOS 9、10和11。设备测试。 where as prefs:仅适用于iOS 9。


在iOS 10.3上,App-Specific URL Schemes的第一个响应对我来说很有效。

if let appSettings = URL(string: UIApplicationOpenSettingsURLString + Bundle.main.bundleIdentifier!) {
    if UIApplication.shared.canOpenURL(appSettings) {
      UIApplication.shared.open(appSettings)
    }
  }

斯威夫特4

如果这是你想要的,这可能会占用你应用程序的特定设置。

UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)

使用@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为我获得一般位置设置工作。注:仅在设备上有效。


@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。如果你使用这些,苹果可能会拒绝你的应用,以下是你提交应用时可能会遇到的情况:

Your app uses the "prefs:root=" non-public URL scheme, which is a private entity. The use of non-public APIs is not permitted on the App Store because it can lead to a poor user experience should these APIs change. Continuing to use or conceal non-public APIs in future submissions of this app may result in the termination of your Apple Developer account, as well as removal of all associated apps from the App Store. Next Steps To resolve this issue, please revise your app to provide the associated functionality using public APIs or remove the functionality using the "prefs:root" or "App-Prefs:root" URL scheme. If there are no alternatives for providing the functionality your app requires, you can file an enhancement request.


剪力/ 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”,会导致一些应用被商店拒绝。


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)
    }
  }
}