Ios 单击按钮时如何打开电话设置?

Ios 单击按钮时如何打开电话设置?,ios,swift,settings,Ios,Swift,Settings,我正在尝试在一个应用程序中实现一个功能,当互联网连接不可用时显示警报。 该警报有两个操作(确定和设置),每当用户单击设置时,我都希望以编程方式将它们转到电话设置 我正在使用Swift和Xcode。使用UIApplication.opensetingsurlstring Swift 5.1的更新 override func viewDidAppear(_ animated: Bool) { let alertController = UIAlertController (title: "

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


我正在使用Swift和Xcode。

使用
UIApplication.opensetingsurlstring

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

 }
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
UIApplication.shared.open(URL(string: "App-prefs:Bluetooth")!)
在iOS 8+中,您可以执行以下操作:

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

    let settingsUrl = URL(string: UIApplicationOpenSettingsURLString)!
    UIApplication.shared.open(settingsUrl)
UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!, options: [:], completionHandler: nil)
⚠️ 小心

这个答案基于未记录的API,最近(自iOS12以来),苹果拒绝使用这种方法的应用程序

原始答案如下

Swift 5

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

   UIApplication.shared.open(settingsUrl)

 }
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
UIApplication.shared.open(URL(string: "App-prefs:Bluetooth")!)
Swift 4

    let settingsUrl = URL(string: UIApplicationOpenSettingsURLString)!
    UIApplication.shared.open(settingsUrl)
UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!, options: [:], completionHandler: nil)
注意:以下方法适用于iOS 11以下的所有版本,对于更高版本,应用程序可能会被拒绝,因为它是一个私有API

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

首先,在项目中配置URL方案。您将在目标->信息->URL方案中找到它。单击+按钮并在URL方案中键入prefs

Swift 5

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

   UIApplication.shared.open(settingsUrl)

 }
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
UIApplication.shared.open(URL(string: "App-prefs:Bluetooth")!)
Swift 3

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

UIApplication.sharedApplication().openURL(NSURL(string:"prefs:root=General")!)
目标-C

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=General"]];
下面是所有可用的URL

**关于IOS<12**

  • prefs:root=General&path=About
  • 首选项:根=常规&路径=辅助功能
  • 首选项:根=飞机模式
  • prefs:root=General&path=AUTOLOCK
  • 首选项:根=常规&路径=使用情况/蜂窝\u使用情况
  • prefs:root=亮度
  • prefs:root=蓝牙
  • 首选项:根=常规&路径=日期和时间
  • prefs:root=FACETIME
  • prefs:root=General
  • 首选项:根=常规&路径=键盘
  • prefs:root=CASTLE
  • 首选项:根=城堡&路径=存储和备份
  • 首选项:根=常规&路径=国际
  • prefs:root=位置\服务
  • prefs:root=帐户设置
  • prefs:root=音乐
  • prefs:root=MUSIC&path=EQ
  • prefs:root=MUSIC&path=VolumeLimit
  • 首选项:根=常规&路径=网络
  • 首选项:root=NIKE\u PLUS\u IPOD
  • prefs:root=NOTES
  • prefs:root=NOTIFICATIONS\u ID
  • prefs:root=Phone
  • prefs:root=照片
  • prefs:root=General&path=managedConfiguration列表
  • 首选项:根=常规&路径=重置
  • 首选项:根=声音&路径=铃声
  • prefs:root=Safari
  • 首选项:根=常规&路径=助手
  • prefs:root=声音
  • prefs:root=General&path=SOFTWARE\u UPDATE\u链接
  • prefs:root=STORE
  • prefs:root=TWITTER
  • prefs:root=FACEBOOK
  • prefs:root=General&path=USAGE prefs:root=VIDEO
  • 首选项:根=常规&路径=网络/VPN
  • prefs:root=墙纸
  • prefs:root=WIFI
  • prefs:root=互联网链接
  • 首选项:根=电话和路径=已阻止
  • prefs:root=请勿打扰
在IOS 13上

UIApplication.shared.open(URL(string:"App-Prefs:root=General")!, options: [:], completionHandler: nil)
UIApplication.shared.openURL(URL(string:"prefs:root=General")!)
  • 应用程序优先级:常规路径=关于
  • 应用程序优先:飞机模式
  • 应用程序首选项:常规和路径=自动锁定
  • 应用程序优先:蓝牙
  • 应用程序优先级:常规和路径=日期和时间
  • 应用程序优先:FACETIME
  • 应用程序优先:通用
  • 应用程序优先级:常规路径=键盘
  • 应用程序优先:城堡
  • 应用程序优先级:CASTLE&path=存储和备份
  • 应用程序优先:常规和路径=国际
  • 应用程序优先:音乐
  • 应用程序首选项:注释
  • 应用程序优先:通知\u ID
  • 应用程序优先:手机
  • 应用程序优先:照片
  • 应用程序首选项:常规和路径=ManagedConfiguration列表
  • 应用程序优先级:常规和路径=重置
  • 应用程序优先:声音和路径=铃声
  • 应用程序优先:声音
  • 应用程序优先:常规和路径=软件\u更新\u链接
  • 应用程序优先:商店
  • 应用程序优先:壁纸
  • 应用程序优先:WIFI
  • 应用程序优先:互联网连接
  • 应用程序优先:请勿打扰

    未测试

  • 应用程序首选项:TWITTER(?)

  • 应用程序首选项:FACEBOOK(?)
  • 应用程序优先:NIKE_PLUS_IPOD(?)

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

在模拟器的ios10/Xcode 8中:

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


没有。

我看到了这行代码

UIApplication.sharedApplication() .openURL(NSURL(string:"prefs:root=General")!)
不起作用,它在ios10/Xcode 8中对我不起作用,只是一个小的代码差异,请用

UIApplication.sharedApplication().openURL(NSURL(string:"App-Prefs:root=General")!)
Swift3

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

UIApplication.shared.openURL(URL(string:"App-Prefs:root=General")!)
希望能有帮助。
干杯。

如上@niravdesai所说的应用程序优先。 我发现
App Prefs:
适用于iOS 9、10和11。测试过的设备。 其中as
prefs:
仅适用于iOS 9。

来自的第一个响应适用于iOS 10.3

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

SWIFT 4

    let settingsUrl = URL(string: UIApplicationOpenSettingsURLString)!
    UIApplication.shared.open(settingsUrl)
UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!, options: [:], completionHandler: nil)
这可能需要你的应用程序的特定设置,如果这是你想要的

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

使用@vivek的提示,我开发了一个基于swift3的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"

}


这是非常有帮助的,因为API肯定会改变,您可以重构一次并且非常快

App-Prefs:root=Privacy&path=LOCATION
为我提供了常规位置设置。注:仅适用于设备。

添加到@Luca Davanzo

iOS 11,某些权限设置已移动到应用程序路径:

iOS 11支持


警告:
prefs:root
App-prefs:root
URL方案被视为私有API。如果您使用这些应用程序,苹果可能会拒绝您的应用程序,以下是您在提交应用程序时可能得到的信息:

您的应用程序使用“prefs:root=”非公共URL方案,这是一个私有实体。不允许在App Store上使用非公共API,因为如果这些API发生变化,可能会导致糟糕的用户体验。在本应用程序的未来提交中继续使用或隐藏非公共API可能会导致y的终止