单击按钮时如何打开 phone 设置?

How do I open phone settings when a button is clicked?

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

我正在使用 Swift 和 Xcode。

使用UIApplication.openSettingsURLString

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

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 5

if let settingsUrl = URL(string: UIApplication.openSettingsURLString) {

   UIApplication.shared.open(settingsUrl)

 }

在 iOS 8+ 中,您可以执行以下操作:

 func buttonClicked(sender:UIButton)
    {
        UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString))
    }

Swift 4

    let settingsUrl = URL(string: UIApplicationOpenSettingsURLString)!
    UIApplication.shared.open(settingsUrl)

⚠️ Be careful!

This answer is based on undocumented APIs and recently (since iOS12) Apple is rejecting apps with this approach.

原回答如下

Swift 5

UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)

Swift 4

UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!, options: [:], completionHandler: nil)

注意:以下方法适用于 iOS11 以下的所有版本,对于更高版本,应用可能会被拒绝,因为它是私有的 API

有时我们希望将用户带到应用设置以外的设置。 以下方法将帮助您实现这一目标:

首先,在您的项目中配置 URL 方案。您会在 Target -> Info -> URL Scheme 中找到它。单击 + 按钮并在 URL Schemes

中键入 prefs

Swift 5

UIApplication.shared.open(URL(string: "App-prefs:Bluetooth")!)

Swift 3

UIApplication.shared.open(URL(string:"App-Prefs:root=General")!, options: [:], completionHandler: nil)

Swift

UIApplication.sharedApplication().openURL(NSURL(string:"prefs:root=General")!)

Objective-C

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=General"]];

以下都是可用的URLs

**在 IOS < 12 **

  • prefs:root=一般&路径=关于
  • prefs:root=一般&路径=可访问性
  • prefs:root=AIRPLANE_MODE
  • prefs:root=一般&路径=AUTOLOCK
  • prefs:root=常规&路径=USAGE/CELLULAR_USAGE
  • prefs:root=亮度
  • prefs:root=蓝牙
  • prefs:root=常规&路径=DATE_AND_TIME
  • prefs:root=FACETIME
  • prefs:root=一般
  • prefs:root=常规&路径=键盘
  • prefs:root=城堡
  • prefs:root=城堡&路径=STORAGE_AND_BACKUP
  • prefs:root=一般&路径=国际
  • prefs:root=LOCATION_SERVICES
  • prefs:root=ACCOUNT_SETTINGS
  • prefs:root=音乐
  • prefs:root=音乐&路径=EQ
  • prefs:root=音乐&路径=音量限制
  • prefs:root=常规&路径=网络
  • prefs:root=NIKE_PLUS_IPOD
  • prefs:root=注释
  • prefs:root=NOTIFICATIONS_ID
  • prefs:root=Phone
  • prefs:root=照片
  • prefs:root=常规&路径=ManagedConfigurationList
  • prefs:root=常规&路径=重置
  • prefs:root=声音&路径=铃声
  • prefs:root=野生动物园
  • prefs:root=一般&路径=助理
  • prefs:root=声音
  • prefs:root=常规&路径=SOFTWARE_UPDATE_LINK
  • prefs:root=商店
  • prefs:root=推特
  • prefs:root=脸书
  • prefs:root=一般&路径=用法prefs:root=视频
  • prefs:root=常规&路径=Network/VPN
  • prefs:root=壁纸
  • prefs:root=WIFI
  • prefs:root=INTERNET_TETHERING
  • prefs:root=Phone&path=阻塞
  • prefs:root=DO_NOT_DISTURB

上IOS13

  • App-prefs:General&path=About
  • 应用偏好:AIRPLANE_MODE
  • App-prefs:General&path=AUTOLOCK
  • 应用偏好:蓝牙
  • App-prefs:General&path=DATE_AND_TIME
  • 应用偏好:FACETIME
  • 应用偏好:常规
  • App-prefs:General&path=Keyboard
  • App-prefs:CASTLE
  • App-prefs:CASTLE&path=STORAGE_AND_BACKUP
  • App-prefs:General&path=INTERNATIONAL
  • 应用偏好:音乐
  • App-prefs:NOTES
  • 应用偏好:NOTIFICATIONS_ID
  • 应用偏好:Phone
  • 应用偏好:照片
  • App-prefs:General&path=ManagedConfigurationList
  • App-prefs:General&path=Reset
  • App-prefs:Sounds&path=铃声
  • 应用偏好:声音
  • App-prefs:General&path=SOFTWARE_UPDATE_LINK
  • 应用偏好:商店
  • 应用偏好:壁纸
  • 应用偏好:WIFI
  • 应用偏好:INTERNET_TETHERING
  • 应用偏好:DO_NOT_DISTURB

    未测试

  • App-prefs:TWITTER (??)

  • App-prefs:FACEBOOK (??)
  • 应用偏好:NIKE_PLUS_IPOD (??)

注意:网络设置不会在模拟器中打开,但link可以在真实设备上使用。

in 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。经过测试的设备。 其中 prefs: 仅适用于 iOS 9.

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

SWIFT 4

这可能会采用您应用的特定设置,如果您正在寻找的话。

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

根据@vivek 的提示,我开发了一个基于 Swift 3 的实用程序 class,希望您能欣赏!

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

iOS11、部分权限设置已移至app路径:

iOS 11 Support

 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:rootApp-Prefs:root URL 方案被认为是私有的 API。如果您使用这些应用程序,Apple 可能会拒绝您的应用程序,这是您在提交应用程序时可能会得到的信息:

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.

iOS 12+

open(url:options:completionHandler:) 方法已更新为包含一个非零选项字典,截至此 post 仅包含类型 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”,一些应用已被商店拒绝。