Ich versuche, die Startoption zu handhaben und einen bestimmten View-Controller zu öffnen, nachdem ich eine Remotemeldung getippt habe, die ich in Swift 3 erhalte. Ich habe eine ähnliche Frage gesehen, zum Beispiel here , aber nichts für die neue Swift 3-Implementierung. Ich habe eine ähnliche Frage (und) in AppDelegate.Swift gesehen. Ich habe folgendes in didFinishLaunchingWithOptions:
var localNotif = (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as! String)
if localNotif {
var itemName = (localNotif.userInfo!["aps"] as! String)
print("Custom: \(itemName)")
}
else {
print("//////////////////////////")
}
aber Xcode gibt mir diesen Fehler:
Type '[NSObject: AnyObject]?' has no subscript members
Ich habe auch folgendes versucht:
if let launchOptions = launchOptions {
var notificationPayload: NSDictionary = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as NSDictionary!
}
und ich bekomme diesen Fehler:
error: ambiguous reference to member 'subscript'
Ähnliche Fehler bekam ich überall dort, wo ich zuvor ähnlichen Code verwendet hatte, um einen Wert mit einem Schlüssel aus einem Wörterbuch abzurufen, und ich musste die Codes ersetzen und das Wörterbuch grundsätzlich sicher auspacken. Aber das scheint hier nicht zu funktionieren. Jede Hilfe wäre dankbar. Vielen Dank.
Es stellte sich also heraus, dass sich die gesamte Methodensignatur geändert hat und als ich die neue Signatur implementierte, funktionierte alles gut. Unten ist der Code.
neue didFinishLaunchingWithOptions-Methode:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
//and then
if launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] != nil {
// Do what you want to happen when a remote notification is tapped.
}
}
Hoffe das hilft.
Apple hat zahlreiche Änderungen an Swift 3
vorgenommen, und dies hier.
Edit: Dies funktioniert auch für Swift 4.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//Launched from Push notification
let remoteNotif = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String: Any]
if remoteNotif != nil {
let aps = remoteNotif!["aps"] as? [String:AnyObject]
NSLog("\n Custom: \(String(describing: aps))")
}
else {
NSLog("//////////////////////////Normal launch")
}
}
Weitere Informationen zu LaunchOptionKeys
finden Sie in der Dokumentation von Apple .
// Check if launched from the remote notification and application is close
if let remoteNotification = launchOptions?[.remoteNotification] as? [AnyHashable : Any] {
// Do what you want to happen when a remote notification is tapped.
let aps = remoteNotification["aps" as String] as? [String:AnyObject]
let apsString = String(describing: aps)
debugPrint("\n last incoming aps: \(apsString)")
}
Swift 3:
if let notification = launchOptions?[.localNotification] as? NSDictionary{
#if DEBUG
print("iOS9 didFinishLaunchingWithOptions notification\n \(notification)")
#endif
if let remoteNotif = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String: Any] {
if let notification = remoteNotif["aps"] as? [AnyHashable : Any] {
//handle PN
}
}