Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle launch options in Swift 3 when a notification is tapped? Getting syntax problems

I am trying to handle the launch option and open a specific view controller upon tapping a remote notification that I receive in swift 3. I have seen similar question, for instance here, but nothing for the new swift 3 implementation. I saw a similar question (and ) In AppDelegate.swift I have the following in didFinishLaunchingWithOptions:

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

but Xcode is giving me this error:

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

I also tried this:

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

    }

and I get this error:

error: ambiguous reference to member 'subscript'

I got similar errors wherever I had previously used similar code to get a value from a dictionary by the key and I had to replace the codes and basically safely unwrap the dictionary first. But that doesn't seem to work here. Any help would be appreciated. Thanks.

like image 798
TheeBen Avatar asked Oct 24 '16 00:10

TheeBen


3 Answers

Apple made plenty of changes in Swift 3 and this one of them.

Edit: This works for Swift 4 as well.

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

Swift 5:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    //Launched from push notification
    guard let options = launchOptions,
        let remoteNotif = options[UIApplication.LaunchOptionsKey.remoteNotification] as? [String: Any]
        else {
            return
    }
    let aps = remoteNotif["aps"] as? [String: Any]
    NSLog("\n Custom: \(String(describing: aps))")
    
    handleRemoteNotification(remoteNotif)
}

And for more on LaunchOptionsKey read Apple's documentation.

like image 52
Adeel Miraj Avatar answered Oct 13 '22 01:10

Adeel Miraj


So it turned out the whole method signature has changed and when I implemented the new signature things worked just fine. Below is the code.

new didFinishLaunchingWithOptions method:

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.


}

}

Hope this helps.

like image 40
TheeBen Avatar answered Oct 13 '22 00:10

TheeBen


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)")
    }
like image 33
Amr Angry Avatar answered Oct 13 '22 00:10

Amr Angry