Ich versuche, eine Funktion in einer App zu implementieren, die eine Warnmeldung anzeigt, wenn die Internetverbindung nicht verfügbar ist .. Die Warnmeldung hat zwei Aktionen (OK und Einstellungen). Wenn ein Benutzer auf Einstellungen klickt, möchte ich sie zur Telefoneinstellungen programmgesteuert.
Ich verwende Swift und Xcode.
UIApplication.openSettingsURLString
verwenden
Update für 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)
}
Swift 2.x
UIApplicationOpenSettingsURLString
verwenden
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)
}
Swift 4
UIApplication.shared.open(URL.init(string: UIApplicationOpenSettingsURLString)!, options: [:], completionHandler: nil)
HINWEIS: Die folgende Methode funktioniert für alle Versionen unter iOS 11; für höhere Versionen wird die App möglicherweise abgelehnt, da es sich um eine private API handelt.
Manchmal möchten wir einen Benutzer zu anderen Einstellungen als unseren App-Einstellungen bringen. Die folgende Methode hilft Ihnen dabei, dies zu erreichen:
Konfigurieren Sie zunächst die URL-Schemas in Ihrem Projekt. Sie finden es unter Ziel -> Info -> URL-Schema. Klicken Sie auf die Schaltfläche + und geben Sie die Präferenzen in URL-Schemata ein
Swift 3
UIApplication.shared.open(URL(string:"App-Prefs:root=General")!, options: [:], completionHandler: nil)
Schnell
UIApplication.sharedApplication().openURL(NSURL(string:"prefs:root=General")!)
Ziel c
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=General"]];
und im Folgenden sind alle verfügbaren URLs aufgeführt
Hinweis: Die Netzwerkeinstellung wird nicht in einem Simulator geöffnet, der Link funktioniert jedoch auf einem echten Gerät.
In iOS 8+ können Sie Folgendes tun:
func buttonClicked(sender:UIButton)
{
UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString))
}
Swift 4
let settingsUrl = URL(string: UIApplicationOpenSettingsURLString)!
UIApplication.shared.open(settingsUrl)
Mit dem Hinweis von @ vivek entwickle ich eine Utils-Klasse basierend auf Swift 3 , hoffe, dass Sie es schätzen!
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)
}
}
}
Dies ist sehr hilfreich, da sich die APIs auf jeden Fall ändern werden und Sie einmal und sehr schnell umgestalten können!
Die erste Antwort aus App-spezifischen URL-Schemata hat bei mir auf iOS 10.3 funktioniert.
if let appSettings = URL(string: UIApplicationOpenSettingsURLString + Bundle.main.bundleIdentifier!) {
if UIApplication.shared.canOpenURL(appSettings) {
UIApplication.shared.open(appSettings)
}
}
App-Prefs:root=Privacy&path=LOCATION
arbeitete für mich, um zu den allgemeinen Standorteinstellungen zu gelangen. Hinweis: Funktioniert nur auf einem Gerät.
Ich habe diese Codezeile gesehen
UIApplication.sharedApplication() .openURL(NSURL(string:"prefs:root=General")!)
funktioniert nicht, es hat bei mir in ios10/Xcode 8 nicht funktioniert, nur ein kleiner Codeunterschied, bitte ersetzen Sie dies durch
UIApplication.sharedApplication().openURL(NSURL(string:"App-Prefs:root=General")!)
Swift3
UIApplication.shared.openURL(URL(string:"prefs:root=General")!)
Ersetzen mit
UIApplication.shared.openURL(URL(string:"App-Prefs:root=General")!)
Ich hoffe, es hilft.
in ios10/Xcode 8 im Simulator:
UIApplication.shared.openURL(URL(string:UIApplicationOpenSettingsURLString)!)
funktioniert
UIApplication.shared.openURL(URL(string:"prefs:root=General")!)
nicht.
Swift 4.2, iOS 12
Die open(url:options:completionHandler:)
-Methode wurde um ein Nicht-Null-Optionswörterbuch erweitert, das ab diesem Beitrag nur eine mögliche Option des Typs UIApplication.OpenExternalURLOptionsKey
enthält (im Beispiel).
@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)
}
Das explizite Erstellen einer URL, wie z. B. mit "App-Prefs", hat AFAIK dazu gebracht, einige Apps aus dem Store abzulehnen.
Hinzufügen zu @Luca Davanzo
in iOS 11 wurden einige Berechtigungseinstellungen in den App-Pfad verschoben:
Unterstützung für 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)
}
}
Warnhinweis: Die URL-Schemata prefs:root
oder App-Prefs:root
gelten als private API. Apple lehnt Ihre App möglicherweise ab, wenn Sie diese verwenden. Folgendes erhalten Sie beim Senden Ihrer App:
Ihre App verwendet das nicht öffentliche URL-Schema "prefs: root =", bei dem es sich um eine private Entität handelt. Die Verwendung nicht öffentlicher APIs ist im App Store nicht zulässig, da dies zu einer schlechten Benutzererfahrung führen kann, falls sich diese APIs ändern. Wenn Sie in zukünftigen Einsendungen dieser App nicht-öffentliche APIs weiterhin verwenden oder verbergen, kann dies dazu führen, dass Ihr Apple Developer-Konto gekündigt wird und alle zugehörigen Apps aus dem App Store entfernt werden.
Nächste Schritte
Um dieses Problem zu beheben, überarbeiten Sie Ihre App, um die zugehörige Funktionalität mithilfe öffentlicher APIs bereitzustellen, oder entfernen Sie die Funktion mithilfe des URL-Schemas "prefs: root" oder "App-Prefs: root".
Wenn es keine Alternativen gibt, um die Funktionalität bereitzustellen, die Ihre App benötigt, können Sie eine Verbesserungsanforderung einreichen.
Swift 4
Dies kann bestimmte Einstellungen Ihrer App erfordern, wenn Sie danach suchen.
UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
Wie oben @niravdesai sagte App-prefs. Ich habe festgestellt, dass App-Prefs:
sowohl für iOS 9, 10 als auch für 11. getestete Geräte funktioniert. wobei as prefs:
nur unter iOS 9 funktioniert.