web-dev-qa-db-fra.com

Comment gérer les options de lancement dans Swift 3 lorsqu'une notification est exploitée? Obtenir des problèmes de syntaxe

J'essaie de gérer l'option de lancement et d'ouvrir un contrôleur de vue spécifique en appuyant sur une notification distante reçue dans Swift 3. J'ai vu une question similaire, par exemple ici , mais rien pour la nouvelle implémentation de Swift 3. J'ai vu une question similaire (et) dans AppDelegate.Swift, j'ai les éléments suivants dans didFinishLaunchingWithOptions: 

    var localNotif = (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as! String)
if localNotif {
    var itemName = (localNotif.userInfo!["aps"] as! String)
    print("Custom: \(itemName)")
}
else {
    print("//////////////////////////")
}

mais Xcode me donne cette erreur: 

Type '[NSObject: AnyObject]?' has no subscript members

J'ai aussi essayé ceci: 

   if let launchOptions = launchOptions {
        var notificationPayload: NSDictionary = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as NSDictionary!

    }

et j'obtiens cette erreur: 

error: ambiguous reference to member 'subscript'

Des erreurs similaires se produisaient partout où j'avais précédemment utilisé un code similaire pour obtenir une valeur d'un dictionnaire à l'aide de la clé. Je devais remplacer les codes et, fondamentalement, décompresser le dictionnaire en toute sécurité. Mais cela ne semble pas fonctionner ici. Toute aide serait appréciée. Merci. 

14
TheeBen

La signature de la méthode entière a donc changé et lorsque j'ai implémenté la nouvelle signature, tout a bien fonctionné. Ci-dessous le code. 

nouvelle méthode didFinishLaunchingWithOptions: 

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.


}

}

J'espère que cela t'aides. 

13
TheeBen

Apple a apporté beaucoup de changements dans Swift 3 et celui-ci.

Edit: Ceci fonctionne également pour 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")
    }
}

Et pour plus d'informations sur LaunchOptionKeys, consultez le documentation } d'Apple.

14
Adeel

Swift 4

// 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)")
    }
5
Amr Angry

Swift 3:

        if let notification = launchOptions?[.localNotification] as? NSDictionary{

            #if DEBUG
                print("iOS9 didFinishLaunchingWithOptions notification\n \(notification)")
            #endif
0
ingconti
if let remoteNotif = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String: Any] {
       if let notification = remoteNotif["aps"] as? [AnyHashable : Any] {
               //handle PN
       }
}
0
Svitlana