Search code examples
iosswiftshortcut

Open ShortCut file pragmatically


I have shortcut file was added as a file in my Xcode project and I need that open this file from my app programmatically.

I try with UIApplication.shared.open but the file not open.

if let url = Bundle.main.url(forResource: "saverone", withExtension: "shortcut", subdirectory:"") {
                UIApplication.shared.open(url.absoluteString, completionHandler: { (success) in
                    print("Settings opened: \(success)") // Prints true
                })
            }

I edit the plist file:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>shortcuts</string>
    <string>file</string>
    <string>file:///private/var/containers/Bundle/Application/45D0D372-725B-43A2-9C32-AE9403AD88FF/Saverone.app/</string>
    <string>Saverone.app/</string>
    <string>saverone.shortcut</string>
    <string>shortcuts://</string>
    <string>file:///private/var/containers/Bundle/Application/111111-725B-43A2-9C32-AE9403AD88FF/Saverone.app/saverone.shortcut</string>
    <string>private/var/containers/Bundle/Application/111111-5E8E-4695-845D-D55AD6526673/Saverone.app/saverone.shortcut</string>
</array>

it is possible ?


Solution

  • Use the iCloud URL instead of the local file, this way you will open the shortcut directly:

    @IBAction func openShourtcut(_ sender: Any) {
        
        guard let url = URL(string: "https://www.icloud.com/shortcuts/6665f86fa0b0449b8e1538e2701960f9") else {
            return //be safe
        }
        
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(url)
        }
    }