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

时间:2015-01-26 14:52:29

标签: ios swift settings

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

我正在使用Swift和Xcode。

13 个答案:

答案 0 :(得分:236)

使用UIApplicationOpenSettingsURLString

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 2.x

使用UIApplicationOpenSettingsURLString

override func viewDidAppear(animated: Bool) {
    var alertController = UIAlertController (title: "Title", message: "Go to Settings?", preferredStyle: .Alert)

    var settingsAction = UIAlertAction(title: "Settings", style: .Default) { (_) -> Void in
        let settingsUrl = NSURL(string: UIApplicationOpenSettingsURLString)
        if let url = settingsUrl {
            UIApplication.sharedApplication().openURL(url)
        }
    }

    var cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: nil)
    alertController.addAction(settingsAction)
    alertController.addAction(cancelAction)

    presentViewController(alertController, animated: true, completion: nil)
}

答案 1 :(得分:192)

Swift 5

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

Swift 4

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

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

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

首先,在项目中配置URL Schemes。你会在Target中找到它 - >信息 - >网址方案。单击+按钮并在URL方案中键入prefs

enter image description here

Swift 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"]];

以下是所有可用的网址

  • prefs:root = General&amp; path = About
  • 首选项:根=通用及安培;路径= ACCESSIBILITY
  • prefs:root = AIRPLANE_MODE
  • 首选项:根=通用及安培;路径= AUTOLOCK
  • prefs:root = General&amp; path = USAGE / CELLULAR_USAGE
  • 首选项:根=亮度
  • 首选项:根=蓝牙
  • prefs:root = General&amp; path = DATE_AND_TIME
  • 首选项:根= FACETIME
  • prefs:root = General
  • prefs:root = General&amp; path = Keyboard
  • 首选项:根= CASTLE
  • 首选项:根=城堡&安培;路径= STORAGE_AND_BACKUP
  • prefs:root = General&amp; path = INTERNATIONAL
  • 首选项:根= LOCATION_SERVICES
  • prefs:root = ACCOUNT_SETTINGS
  • prefs:root = MUSIC
  • 首选项:根=音乐与安培;路径= EQ
  • prefs:root = MUSIC&amp; path = VolumeLimit
  • 首选项:根=通用及安培;路径=网络
  • prefs:root = NIKE_PLUS_IPOD
  • 首选项:根= NOTES
  • prefs:root = NOTIFICATIONS_ID
  • prefs:root = Phone
  • 首选项:根=照片
  • 首选项:根=通用及安培;路径= ManagedConfigurationList
  • prefs:root = General&amp; path = Reset
  • 首选项:根=声音和安培;路径=铃声
  • prefs:root = Safari
  • prefs:root = General&amp; path = Assistant
  • 首选项:根=声音
  • prefs:root = General&amp; path = SOFTWARE_UPDATE_LINK
  • 首选项:根= STORE
  • 首选项:根= TWITTER
  • prefs:root = FACEBOOK
  • prefs:root = General&amp; path = USAGE prefs:root = VIDEO
  • prefs:root = General&amp; path = Network / VPN
  • 首选项:根=壁纸
  • prefs:root = WIFI
  • 首选项:根= INTERNET_TETHERING
  • 首选项:根=电话及安培;路径=阻止
  • 首选项:根= DO_NOT_DISTURB

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

答案 2 :(得分:41)

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

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

Swift 4

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

答案 3 :(得分:20)

使用@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肯定会发生变化,你可以快速重构一次!

答案 4 :(得分: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)
    }
  }

答案 5 :(得分:13)

App-Prefs:root=Privacy&path=LOCATION让我参与了一般的位置设置。注意:仅适用于设备。

答案 6 :(得分:7)

在模拟器中的ios10 / Xcode 8中:

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

作品

UIApplication.shared.openURL(URL(string:"prefs:root=General")!)

没有。

答案 7 :(得分:7)

我看过这行代码

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

希望它有所帮助。 欢呼声。

答案 8 :(得分:6)

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)

}

AFAIK明确构造了URL,例如使用“ App-Prefs”,导致一些应用被商店拒绝。

答案 9 :(得分:5)

添加到@Luca Davanzo

iOS 11,某些权限设置已移至应用路径:

  

iOS 11支持

string GetWebServiceUrl() {
    XmlDocument doc = new XmlDocument();
    string absolutePath = this.Host.ResolvePath("../../../web.config");                
    doc.Load(absolutePath);
    XmlNode node = doc.DocumentElement.SelectSingleNode("/configuration/appSettings/add[@key='ServerServiceURL']");
    return node.Attributes["value"].Value.ToString();
}

答案 10 :(得分:4)

警告语:prefs:rootApp-Prefs:root URL方案被视为私有API。如果您使用这些应用程序,Apple可能会拒绝您的应用程序,这是您提交应用程序时可能会得到的:

  

您的应用程序使用“ prefs:root =“非公开URL方案,这是一个私有实体。在App Store上不允许使用非公共API,因为如果这些API发生更改,可能会导致不良的用户体验。在以后提交此应用程序时继续使用或隐藏非公共API可能会导致您的Apple Developer帐户终止,并从App Store中删除所有关联的应用程序。

     

后续步骤

     

要解决此问题,请修改您的应用程序以使用公共API提供相关功能,或使用“ prefs:root”或“ App-Prefs:root” URL方案删除该功能。

     

如果没有其他选择可提供您的应用所需的功能,则可以提出增强请求。

答案 11 :(得分:2)

SWIFT 4

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

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

答案 12 :(得分:0)

如上所述@niravdesai说App-prefs。 我发现App-Prefs:适用于iOS 9,10和11设备。 其中prefs:仅适用于iOS 9。