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

Sol*_*ola 144 settings ios swift

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

我正在使用Swift和Xcode.

Mar*_*anu 271

运用 UIApplication.openSettingsURLString

更新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: 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)
}
Run Code Online (Sandbox Code Playgroud)

Swift 2.x

运用 UIApplication.openSettingsURLString

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)
}
Run Code Online (Sandbox Code Playgroud)

  • @PersianBlue在iOS 10.1.1中是正确的,无法启动"设置"应用. (5认同)
  • 不,我不这么认为.要转到您的应用程序,您的应用程序应实现自定义URL方案,并从当前应用程序调用`UIApplication.sharedApplication().openURL(yourCustomURL)`,但您没有从Settings应用程序访问 (2认同)

viv*_*ani 221

斯威夫特4

UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
Run Code Online (Sandbox Code Playgroud)

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

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

首先,在项目中配置URL Schemes.您可以在Target - > Info - > URL Scheme中找到它.单击+按钮并在URL Schemes中键入prefs

在此输入图像描述

斯威夫特3

UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!, options: [:], completionHandler: nil)
Run Code Online (Sandbox Code Playgroud)

迅速

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

Objective-C的

UIApplication.sharedApplication().openURL(NSURL(string:"prefs:root=General")!)
Run Code Online (Sandbox Code Playgroud)

以下是所有可用的URL

  • 首选项:根=通用及路径=关于
  • 首选项:根=通用及路径= ACCESSIBILITY
  • 首选项:根= AIRPLANE_MODE
  • 首选项:根=通用及路径= AUTOLOCK
  • 首选项:根=通用及路径= USAGE/CELLULAR_USAGE
  • 首选项:根=亮度
  • 首选项:根=蓝牙
  • 首选项:根=通用及路径= DATE_AND_TIME
  • 首选项:根= FACETIME
  • 首选项:根=通用
  • 首选项:根=通用及路径=键盘
  • 首选项:根= CASTLE
  • 首选项:根=城堡&路径= STORAGE_AND_BACKUP
  • 首选项:根=通用及路径= INTERNATIONAL
  • 首选项:根= LOCATION_SERVICES
  • 首选项:根= ACCOUNT_SETTINGS
  • 首选项:根= MUSIC
  • 首选项:根=音乐与路径= EQ
  • 首选项:根=音乐与路径= VolumeLimit
  • 首选项:根=通用及路径=网络
  • 首选项:根= NIKE_PLUS_IPOD
  • 首选项:根= NOTES
  • 首选项:根= NOTIFICATIONS_ID
  • 首选项:根=电话
  • 首选项:根=照片
  • 首选项:根=通用及路径= ManagedConfigurationList
  • 首选项:根=通用及路径=复位
  • 首选项:根=声音和路径=铃声
  • 首选项:根= Safari浏览器
  • 首选项:根=通用及路径=助理
  • 首选项:根=声音
  • 首选项:根=通用及路径= SOFTWARE_UPDATE_LINK
  • 首选项:根= STORE
  • 首选项:根= TWITTER
  • 首选项:根= FACEBOOK
  • prefs:root = General&path = USAGE prefs:root = VIDEO
  • 首选项:根=通用及路径=网络/ VPN
  • 首选项:根=壁纸
  • 首选项:根= WIFI
  • 首选项:根= INTERNET_TETHERING
  • 首选项:根=电话及路径=阻止
  • 首选项:根= DO_NOT_DISTURB

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

  • 自iOS 12即将推出以来,Apple拒绝了包含此类优势的app.请注意它.甚至我的应用也被拒绝了 (19认同)
  • 对于iOS 10,使用`App-Prefs`而不是`prefs`作为URL字符串的方案.例如`App-Prefs:root = WIFI` (18认同)
  • 为了防止其他人想知道,"CASTLE"是iCloud (14认同)
  • 这不适用于iOS 10.在iOS 9.3上运行相同的代码.它工作,在iOS 10上运行,它没有(无论使用哪个网址) (7认同)
  • 不要使用`App-Prefs`或`prefs:root`,因为这些是私有API,除非你很幸运,你的应用程序将被拒绝. (6认同)
  • 最近给了一个应用程序进行审查,以便在App Store中发布它,Apple因为使用了"prefs:root"或"App-Prefs:root"而拒绝了它.当用户没有互联网连接时,我用它来打开wifi设置. (5认同)
  • 这不再起作用了.应用程序被拒绝了 (5认同)
  • iOS 10的任何解决方案? (4认同)
  • 只需在没有特定页面的情况下打开设置:"prefs:root" (3认同)
  • 有趣的是,我刚刚在ios 7和9上进行了测试.它有效! (3认同)
  • 我们只是因为使用私有 api 而被拒绝。 (2认同)
  • @ EICaptainv2.0,现在所有`App-Prefs`和`pref`现在都是非公共网址.最好的处理方法是,显示弹出窗口,显示导航步骤[GIF或图像幻灯片]然后当用户单击"确定"时,导致使用此"UIApplicationOpenSettingsURLString"设置 (2认同)

Chr*_*örz 43

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

 func buttonClicked(sender:UIButton)
    {
        UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString))
    }
Run Code Online (Sandbox Code Playgroud)

斯威夫特4

    let settingsUrl = URL(string: UIApplicationOpenSettingsURLString)!
    UIApplication.shared.open(settingsUrl)
Run Code Online (Sandbox Code Playgroud)

  • 这将带您进入应用程序特定设置,如何将它们带到常规设置页面,甚至更好地进入健康部分? (13认同)
  • Swift 3 - `UIApplication.shared.openURL(URL(string:UIApplicationOpenSettingsURLString)!)` (2认同)

Luc*_*nzo 21

使用@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"

}
Run Code Online (Sandbox Code Playgroud)
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)
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

这非常有用,因为API肯定会发生变化,你可以快速重构一次!

  • 这会经过苹果审查吗? (4认同)
  • 这不起作用,因为我们从应用程序收到此消息:"您的应用程序使用"prefs:root ="非公共URL方案,这是一个私有实体.在App Store上不允许使用非公共API,因为如果这些API发生变化,可能会导致糟糕的用户体验.在此应用程序的未来提交中继续使用或隐藏非公共API可能会导致Apple Developer帐户终止,以及从App中删除所有相关应用程序商店." (2认同)

inc*_*don 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)
    }
  }
Run Code Online (Sandbox Code Playgroud)


Joe*_*ick 13

App-Prefs:root=Privacy&path=LOCATION为我进入一般的位置设置工作.注意:仅适用于设备.

  • 这被拒绝了 (10认同)

ing*_*nti 7

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

UIApplication.shared.openURL(URL(string:UIApplicationOpenSettingsURLString)!)
Run Code Online (Sandbox Code Playgroud)

作品

UIApplication.shared.openURL(URL(string:"prefs:root=General")!)
Run Code Online (Sandbox Code Playgroud)

才不是.

  • 这仅适用于您的应用有.bundle文件(您的应用偏好设置) (2认同)

nir*_*i21 7

我见过这行代码

UIApplication.sharedApplication() .openURL(NSURL(string:"prefs:root=General")!)
Run Code Online (Sandbox Code Playgroud)

不工作,它在ios10/Xcode 8中对我不起作用,只是一个小的代码差异,请替换它

UIApplication.sharedApplication().openURL(NSURL(string:"App-Prefs:root=General")!)
Run Code Online (Sandbox Code Playgroud)

Swift3

UIApplication.shared.openURL(URL(string:"prefs:root=General")!)
Run Code Online (Sandbox Code Playgroud)

用...来代替

UIApplication.shared.openURL(URL(string:"App-Prefs:root=General")!)
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.干杯.

  • 原因:应用程序被拒绝 (2认同)

ofe*_*fer 7

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

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

下一步

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

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


bso*_*sod 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)

}
Run Code Online (Sandbox Code Playgroud)

明确构建一个URL,例如"App-Prefs",AFAIK已经从商店中删除了一些应用程序.


Tal*_*ion 5

添加到@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)
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 当我运行此代码时,我只是带我进入设置页面.不是它中的特定设置选项卡. (4认同)
  • 可以通过Apple Review吗?我的意思是,这会导致应用被拒绝吗? (2认同)