0

In the Shortcuts app, there is an action called "Open App" (under the Scripting category). How can I run that in Swift?

I looked at AppIntents and SiriKit but just got way too confused, having a hard time figuring out the code.

  • Any app. The "Open App" action can accept a string and if you pass in the exact app name (e.g. "Notes" or "Facebook"), it will open the app. – Ezra Magaram Apr 27 '23 at 10:22
  • 1
    If you want to open an app with a particular *bundle id*, which ***is*** unique. See [this question](https://stackoverflow.com/questions/7229646/open-application-with-bundle-identifier). That said, this is kind of a weird thing to do - since you don't even know whether the app exists. Perhaps an [XY Problem](http://xyproblem.info/)? – Sweeper Apr 27 '23 at 10:31
  • If you hold your finger down on the "App" parameter for the "Open App" action, it lets you select a magic variable. This variable can be a "Text" document or anything you want. – Ezra Magaram Apr 27 '23 at 10:31
  • I will look into the bundle id method, although that solution makes more sense, it might get rejected by Apple. That's why if possible, I'd like to do the Shortcuts solution – Ezra Magaram Apr 27 '23 at 10:34
  • 1
    Okay, I see. Seems like it just doesn't do anything when the name is invalid. I assume it will do the same if the name is ambiguous. Anyway, then it's not possible, as the answers in the linked post says. – Sweeper Apr 27 '23 at 10:36
  • Okay, thank you! Is there no way to call a shortcut using swift? – Ezra Magaram Apr 27 '23 at 10:42
  • 1
    That's a different question. You can [run an existing shortcut](https://support.apple.com/en-hk/guide/shortcuts/apd624386f42/ios) with a URL, but I'm not sure if it can also be used to run Shortcut *actions*. – Sweeper Apr 27 '23 at 10:44

1 Answers1

1

This question, or very similar questions, have been answered before. The general gist of things is that you need to (for security reasons) pre-declare the URL(s) of the app(s) you want to launch from your app in your Info.plist file:

enter image description here

Then you'll be able to call 's openURL function:

UIApplication.shared.openURL(URL(string: "someapp://")!, options: [:])

This should launch the app, although it may prompt the user with an alert to confirm that they want to launch it.

Other than that, there's really no way to do this for security and privacy reasons. You need to declare what apps you want to open because Apple found that some companies were using the openURL API to determine if user's had certain apps installed, then using that to target ads to them. Plus, if any app could willy-nilly launch whatever process they wanted, that'd be a huge additional attack surface in the OS.

Important note: you should be able to launch certain system apps like Maps or Phone without needing to declare them in your Info.plist (e.g. tel:1005551000 will confirm that the user wants to call the number, then place the call).

Sam Spencer
  • 8,492
  • 12
  • 76
  • 133