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

我使用Swift和Xcode。


当前回答

我看过这行代码

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

希望能有所帮助。 欢呼。

其他回答

斯威夫特4

如果这是你想要的,这可能会占用你应用程序的特定设置。

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

使用@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-Prefs:root=Privacy&path=LOCATION为我获得一般位置设置工作。注:仅在设备上有效。

我看过这行代码

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

希望能有所帮助。 欢呼。

在ios10/ Xcode 8模拟器:

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

作品

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

没有。