uioporqwerty / capacitor-app-actions

Capacitor iOS and Android Plugin for App Actions
MIT License
4 stars 7 forks source link

Support didFinishLaunchingWithOptions mode #6

Open sharifulin opened 1 year ago

sharifulin commented 1 year ago

Hey, thanks for the plugin.

The current logic doesn't support didFinishLaunchingWithOptions correctly, when the app doesn't launch and a user tries to launch it via quick actions first.

I found the solution here: https://medium.com/msapps-development/quick-actions-ios-180e79942595 And adapted it for the plugin in the ionic/capacitor app.

AppDelegate.swift

    var shortcutItemToProcess: UIApplicationShortcutItem?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        if let shortcutItem = launchOptions?[UIApplication.LaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem {
            shortcutItemToProcess = shortcutItem
        }

        return true
    }

    func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
        shortcutItemToProcess = shortcutItem
    }

    func applicationDidBecomeActive(_ application: UIApplication) {

        if let shortcutItem = shortcutItemToProcess {
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { // delay to be sure that app.component is ready
                NotificationCenter.default.post(name: NSNotification.Name("appActionReceived"), object: nil, userInfo: ["actionId" : shortcutItem.type])
            }

            // alert
            // var message = shortcutItem.type

            // let alertController = UIAlertController(title: "Quick Action Selected", message: message, preferredStyle: .alert)
            // alertController.addAction(UIAlertAction(title: "Close", style: .default, handler: nil))
            // DispatchQueue.main.async { [unowned self] in
            //     self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
            // }

            //reset the shortcut item
            shortcutItemToProcess = nil
        }
    }

I hope it'll help.

Bokitza commented 2 months ago

Hi Sharif - do you have a working fork that i can check out? You described exactly my problem (: