繁体   English   中英

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

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

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

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

⚠️小心!

此答案基于未记录的 API,最近(自 iOS12 起)Apple 拒绝使用这种方法的应用程序。

原答案如下

斯威夫特 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 Schemes。 您可以在 Target -> Info -> URL Scheme 中找到它。 单击 + 按钮并在 URL Schemes 中键入 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")!)

目标-C

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

以下是所有可用的 URL

**在 IOS < 12 上 **

  • prefs:root=General&path=关于
  • prefs:root=General&path=ACCESSIBILITY
  • 首选项:root=AIRPLANE_MODE
  • prefs:root=General&path=AUTOLOCK
  • prefs:root=General&path=USAGE/CELLULAR_USAGE
  • 首选项:根=亮度
  • 首选项:root=蓝牙
  • 首选项:root=General&path=DATE_AND_TIME
  • 首选项:root=FACETIME
  • 首选项:root=一般
  • prefs:root=General&path=Keyboard
  • 首选项:root = CASTLE
  • 首选项:root=CASTLE&path=STORAGE_AND_BACKUP
  • prefs:root=General&path=INTERNATIONAL
  • 首选项:root=LOCATION_SERVICES
  • 首选项:root = ACCOUNT_SETTINGS
  • 首选项:root=音乐
  • 首选项:root=音乐&路径=EQ
  • prefs:root=MUSIC&path=VolumeLimit
  • prefs:root=General&path=Network
  • 首选项:root=NIKE_PLUS_IPOD
  • 首选项:root=注意
  • 首选项:root=NOTIFICATIONS_ID
  • 首选项:root=电话
  • 首选项:root=照片
  • prefs:root=General&path=ManagedConfigurationList
  • prefs:root=General&path=Reset
  • prefs:root=声音&路径=铃声
  • 首选项:root = Safari
  • prefs:root=General&path=Assistant
  • prefs:root=声音
  • prefs:root=General&path=SOFTWARE_UPDATE_LINK
  • 首选项:root = STORE
  • 首选项:root=推特
  • 首选项:root = FACEBOOK
  • prefs:root=General&path=USAGE prefs:root=VIDEO
  • prefs:root=General&path=网络/VPN
  • 首选项:root=壁纸
  • 首选项:root=WIFI
  • 首选项:root=INTERNET_TETHERING
  • prefs:root=Phone&path=Blocked
  • 首选项:root=DO_NOT_DISTURB

在 IOS 13 上

  • 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
  • 应用偏好:城堡
  • 应用首选项:CASTLE&path=STORAGE_AND_BACKUP
  • App-prefs:General&path=INTERNATIONAL
  • 应用偏好:音乐
  • 应用偏好:注意事项
  • 应用偏好:NOTIFICATIONS_ID
  • 应用偏好:电话
  • 应用偏好:照片
  • App-prefs:General&path=ManagedConfigurationList
  • App-prefs:General&path=Reset
  • App-prefs:Sounds&path=Ringtone
  • 应用偏好:声音
  • App-prefs:General&path=SOFTWARE_UPDATE_LINK
  • 应用偏好:商店
  • 应用偏好:壁纸
  • 应用偏好:WIFI
  • 应用偏好:INTERNET_TETHERING
  • 应用偏好:DO_NOT_DISTURB

    未测试

  • 应用偏好:TWITTER (??)

  • 应用偏好:FACEBOOK (??)
  • 应用偏好:NIKE_PLUS_IPOD (??)

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

快速 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)

使用@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-Specific URL Schemes的第一个响应在 iOS 10.3 上对我有用。

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

App-Prefs:root=Privacy&path=LOCATION为我工作以获取一般位置设置。 注意:仅适用于设备。

警告: 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 方案移除该功能。

如果没有其他方法可以提供您的应用程序所需的功能,您可以提交增强请求。

在模拟器中的 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")!)

斯威夫特3

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

用。。。来代替

UIApplication.shared.openURL(URL(string:"App-Prefs:root=General")!)

希望能帮助到你。 干杯。

斯威夫特 4.2,iOS 12

open(url:options:completionHandler:)方法已更新为包含一个非 nil 选项字典,截至本文,该字典仅包含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”,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)
    }
}

快速 4

如果这就是您要查找的内容,这可能需要您的应用程序的特定设置。

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

如上所述,@niravdesai 说的是 App-prefs。 我发现App-Prefs:适用于 iOS 9、10 和 11。经过测试的设备。 其中 as prefs:仅适用于 iOS 9。

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

可以打开iphone存储设置页面吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM