43

I'm a newbie iPhone developer, writing an app that will kind of be the "mobile version" of a website.

I'm wondering whether it's possible to launch my app from a link in a website. So, for example, someone goes into our site in the iPhone Safari, clicks a link, and our app launches.

Is that possible?
If so can I also "pass parameters" to the app i'm launching? As in...
Clicking different links would allow them to get different stuff in the app at launch time.

Thanks!
Daniel

Daniel Magliola
  • 30,898
  • 61
  • 164
  • 243

3 Answers3

58

Certain apps have URL schemes that will launch them. If an app has published this scheme (or if you dig around in their bundle) you can launch it. For example, a hypothetical twitter app might launch with a twitterapp://here+is+my+tweet URL. If you preload your app with the correct URL scheme, you can do this for yours, too.

Here's a sample URL scheme from the info.plist:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>com.standalone.cooltwitterapp</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>twitterapp</string>
        </array>
    </dict>
</array>
Ben Gottlieb
  • 85,404
  • 22
  • 176
  • 172
  • 1
    was going to answer the same. +1 – Jacob Relkin Jan 19 '10 at 17:50
  • 4
    How does the Youtube app open when you click on a `http://youtube.com/` link (i.e. not `youtube://`) – andrewtweber Feb 28 '13 at 17:04
  • 2
    @andrewtweber the OS specifically handles that: http://developer.apple.com/library/ios/#featuredarticles/iPhoneURLScheme_Reference/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007899 – TofuBeer Mar 03 '13 at 22:19
  • 3
    What happens when this URL is clicked in a desktop web browser? Mac Safari? Mac Chrome? Mac Firefox? Windows IE? Windows mac/chrome/FF? – Amir Memon Mar 05 '13 at 22:06
  • What do you mean with 'this' URL? twitterapp:// ? It probably gives an error or a warning, but you can server-side handle that situation because you know the users platform. You can show a normal link if it is from one of the browsers you listed. – EralpB Feb 19 '14 at 23:46
  • What if the 'myapp://' is not installed on the device? how do I verify it, and send the user to the app store or website instead? – asaf am Sep 02 '14 at 14:50
  • 1
    @asaf am , see the answer from jensgram, below - http://stackoverflow.com/a/2095662 – ban-geoengineering Jun 16 '15 at 12:13
9

Also worth noting is that the application:openURL:sourceApplication:annotation: method will be called when your app launches from a URL. Details at Apple's Docs.

James Kuang
  • 10,710
  • 4
  • 28
  • 38
bmalicoat
  • 2,508
  • 3
  • 24
  • 23
  • 1
    I fixed your link (it was broken). Also their docs say that handleOpenURL is deprecated and that a different openURL function should be used instead. – eodabash Feb 13 '12 at 19:05
  • 1
    That's correct, `handleOpenURL:` is deprecated. Use this `application:openURL:sourceApplication:annotation:` instead. – James Kuang Dec 02 '13 at 20:00
7

Yes you can, using custom URI schemes. Do note, however, that clients without the app will not be able to use the links.

If so can I also "pass parameters" to the app i'm launching?

Again, yes. Once your app has registered for a given scheme it's all yours.

jensgram
  • 31,109
  • 6
  • 81
  • 98
  • 2
    Why not store a cookie saying whether clients have the app, and display one link or the other based on the cookie. If you don't find the cookie, instead display a question: "Do you have XYZ app installed?" If they say "No", offer to install it right away. – iconoclast Apr 18 '12 at 18:32
  • @Ju66ernaut Updated link. Thanks. – jensgram Dec 13 '16 at 10:48