3d touch quick action call UIButton

326 views Asked by At

I have created an app that has 4 UIButtons on the initial VC. Each button segues to another VC(same in all cases), but with different parameters.

 @IBAction func googleBtnPressed(_ sender: AnyObject) {
    searchEngine = "Google"
    print(searchEngine)
    performSegue(withIdentifier: "google", sender: nil)

}

@IBAction func bingBtnPressed(_ sender: AnyObject) {
    searchEngine = "Bing"
    print(searchEngine)
    performSegue(withIdentifier: "google", sender: nil)

}

@IBAction func ddgBtnPressed(_ sender: AnyObject) {
    searchEngine = "DuckDuckGo"
    print(searchEngine)
    performSegue(withIdentifier: "google", sender: nil)

}

@IBAction func customBtnPressed(_ sender: AnyObject) {
    performSegue(withIdentifier: "custom", sender: nil)

}

if let vc = segue.destination as? ViewController {
            if searchEngine == "Google" {
                    vc.url = NSURL(string: "https://www.google.com")
                    vc.segueUsed = "google"
                    vc.searchEngine = "google"
            }

            else if searchEngine == "Bing" {
                    vc.url = NSURL(string: "https://www.bing.com")
                    vc.segueUsed = "google"
                    vc.searchEngine = "bing"
            }

            else if searchEngine == "DuckDuckGo" {
                    vc.url = NSURL(string: "https://www.duckduckgo.com")
                    vc.segueUsed = "google"
                    vc.searchEngine = "duckduckgo"
            }
        }

    }else if segue.identifier == "custom"{
        print(segue.identifier!)
        if let vc = segue.destination as? ViewController {
            vc.segueUsed = "custom"
        }
    }

I want to call these 4 buttons from home screen using 3d Touch's Quick Actions. I have already created entries in the info.plist file, but am struggling while handling the response in AppDelegate.swift

<key>UIApplicationShortcutItems</key>
<array>
    <dict>
        <key>UIApplicationShortcutItemType</key>
        <string>$(PRODUCT_BUNDLE_IDENTIFIER).Google</string>
        <key>UIApplicationShortcutItemTitle</key>
        <string>Google</string>
        <key>UIApplicationShortcutItemIconType</key>
        <string>google</string>
    </dict>
</array>

How do I implement this?

Thanks for help.

1

There are 1 answers

1
Yogesh Mv On
let yourViewcontroller = self.window?.rootViewController// This view controller should be action view controller
        let itemKey = shortcutItem.userInfo["<YOUR KEY>"]
        if itemKey == "Google" {
            //call google action method
        } else if itemKey == "Bing" {
            //call Bing action method
        } else if itemKey == "Ddg" {
            //call "Ddg" action method
        }else if itemKey == "CustomButton" {
            //call CustomButton action method
        }

Note: You can save the itemKey in ShortCutItemDictionary.