Anscheinend wurde OpenURL
in iOS 10 abgeschrieben. Hat jemand eine Dokumentation zu den Gründen oder kann er erklären, was als Nächstes zu tun ist? Ich habe schon auf der Apple-Site nachgeschaut und ein paar Dinge über OpenURL herausgefunden, und das sagen sie jetzt:
UIApplication.shared().open(url: URL, options: [String: AnyObject], completionHandler: ((Bool) -> Void)?)
Hat jemand den Beweis, dass dies die neue Art ist, OpenURL in Swift 3.0 zu verwenden? Welche Werte sollen zusätzlich in den Parametern options:
und completionHandler:
verwendet werden?
Sie können die bedingte Prüfung auch verwenden, wenn Sie Ihre App mit iOS10-kompatiblem Code aktualisieren:
func open(scheme: String) {
if let url = URL(string: scheme) {
if #available(iOS 10, *) {
UIApplication.shared.open(url, options: [:],
completionHandler: {
(success) in
print("Open \(scheme): \(success)")
})
} else {
let success = UIApplication.shared.openURL(url)
print("Open \(scheme): \(success)")
}
}
}
Verwendungszweck:
open(scheme: "tweetbot://timeline")
Eine schnelle Lösung:
// Objective-C
UIApplication *application = [UIApplication sharedApplication];
[application openURL:URL options:@{} completionHandler:nil];
// Swift
UIApplication.shared.open(url, options: [:], completionHandler: nil)
Eine vollständige Antwort:
http://useyourloaf.com/blog/openurl-deprecated-in-ios10/
Credits: Keith Harrison (useyourloaf.com)}
Ein empty options Wörterbuch führt zu demselben Verhalten wie openUrl .
Andernfalls:
+-------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| UIApplicationOpenURLOptionsSourceApplicationKey | NSString containing the bundle ID of the originating application |
+-------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| UIApplicationOpenURLOptionsAnnotationKey | property-list typed object corresponding to what the originating application passed in UIDocumentInteractionController's annotation property |
+-------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| UIApplicationOpenURLOptionsOpenInPlaceKey | bool NSNumber, set to YES if the file needs to be copied before use |
+-------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
Aus UIApplication.h
// Options are specified in the section below for openURL options. An empty options dictionary will result in the same
// behavior as the older openURL call, aside from the fact that this is asynchronous and calls the completion handler rather
// than returning a result.
// The completion handler is called on the main queue.
- (void)openURL:(NSURL*)url options:(NSDictionary<NSString *, id> *)options completionHandler:(void (^ __nullable)(BOOL success))completion NS_AVAILABLE_IOS(10_0) NS_EXTENSION_UNAVAILABLE_IOS("");
UIKIT_EXTERN UIApplicationOpenURLOptionsKey const UIApplicationOpenURLOptionsSourceApplicationKey NS_Swift_NAME(sourceApplication) NS_AVAILABLE_IOS(9_0); // value is an NSString containing the bundle ID of the originating application
UIKIT_EXTERN UIApplicationOpenURLOptionsKey const UIApplicationOpenURLOptionsAnnotationKey NS_Swift_NAME(annotation) NS_AVAILABLE_IOS(9_0); // value is a property-list typed object corresponding to what the originating application passed in UIDocumentInteractionController's annotation property
UIKIT_EXTERN UIApplicationOpenURLOptionsKey const UIApplicationOpenURLOptionsOpenInPlaceKey NS_Swift_NAME(openInPlace) NS_AVAILABLE_IOS(9_0); // value is a bool NSNumber, set to YES if the file needs to be copied before use
Die neue UIApplication-Methode openURL: options: completionHandler: wird asynchron ausgeführt und ruft den angegebenen Completion-Handler in der Hauptwarteschlange auf (diese Methode ersetzt openURL :).
Dies ist unter Zusätzliche Rahmenänderungen> UIKit unter: https://developer.Apple.com/library/prerelease/content/releasenotes/General/WhatsNewIniOS/Articles/iOS10.html
Bevor Sie die URL laden, müssen Sie zunächst einige Überprüfungen durchführen. Bitte überprüfen Sie die folgenden Codes.
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"https://www.gmail.com"]]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.gmail.com"] options:@{} completionHandler:^(BOOL success) {
//completion codes here
}];
}
Ich hoffe das hilft.
Wenn Ihre App weiterhin iOS 9
oder niedriger unterstützt, verwenden Sie einfach die alte openURL
. Sie sollten nur dann zum neuen wechseln, wenn Ihr Implementierungsziel iOS 10
ist.
lassen Sie aktuell:
[String: AnyObject] = ["xxx key": "xxx value" as AnyObject, "yyy key": "yyy value" as AnyObject]
UIApplication.shared.open(URL(string: "http:google.com")!, options: actual, completionHandler: {(true) -> Swift.Void in
print("Refresh")
})
Wobei xxx und yyy eine beliebige Zeichenfolge sind, die Sie drucken möchten oder leer lassen.
sie können die Funktion verwenden, um die Einstellungen zu öffnen:
func showAlert(title:String, message:String){
let alert = UIAlertController(title: title,
message: message,
preferredStyle: UIAlertController.Style.alert)
let okAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alert.addAction(okAction)
let settingsAction = UIAlertAction(title: "Settings", style: .default, handler: { _ in
// Take the user to Settings app to possibly change permission.
guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else { return }
if UIApplication.shared.canOpenURL(settingsUrl) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
// Finished opening URL
})
} else {
// Fallback on earlier versions
UIApplication.shared.openURL(settingsUrl)
}
}
})
alert.addAction(settingsAction)
present(alert, animated: true, completion: nil)
}
Rufen Sie diese Funktion wie folgt auf
showAlert(title: "Unable to access the Photos", message: "To enable access, go to Settings > Privacy > Photos and turn on Photos access for this app.")