47

What I want to do is, We have having one product info on Website. That product is available on store. what we have on website is that, Product info and one button for that product.

I want to take two actions on that button. When User opens website on iPad or iPhone on Safari (browser) and click on GetProduct button, then following two actions must be taken place. 1. If user is already having product installed on device then directly open the app in device. 2. If user is not having the app on device then link user to the app on store, so he can download from there.

I already handled second condition, but how to handle the first condition. If I am already having the app then how to open it on action of button click in browser.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sagar
  • 1,286
  • 3
  • 19
  • 34
  • 1
    check out how to create URL Schemas in iOS and use them. – devluv Sep 17 '14 at 05:48
  • @sagar how did you handle the condition where if the user does not have the app then it opens in the app store? – Brian Risk Jul 13 '17 at 17:51
  • @BrianRisk :- You just have to add the URL of the app which is on store into the section URLScema -> URL. That will automatically check if App is present in device then open it automatically, if not then it will redirect to the AppStore URL – Sagar Aug 02 '17 at 09:14
  • @Sagar Can you please tell which URL schema you are referring to when you are redirecting to the app store URL? – Mr. Ratnadeep Jun 19 '18 at 11:56
  • @Mr.Ratnadeep :- You need to use URL schema along with : and // like this (e.g., "myScheme://") make sure this is case sensitive. Then it will check for app in device not found then it will redirect to iTunes and then match with team id and opens the app on store. – Sagar Jun 20 '18 at 13:16
  • @sagar But if app in not installed on my device, and if I click on URL, it does nothing showing a blank page. Have you written some logic to get it redirected to app store opening your app? – Mr. Ratnadeep Jun 21 '18 at 15:05
  • @Mr.Ratnadeep :- Sorry bro. I haven't written anything to redirect to AppStore. But you can use UniversalLinks for the same. That will check if app is present in device or not. If app found then it opens in device if not then redirect to itunes store – Sagar Jun 22 '18 at 07:46

2 Answers2

68

You can achieve what you're asking for by using a URL scheme. This will enable you to call the openUrl: method with your application's url scheme which will then launch your app. Here's how you setup a custom url scheme:

  1. Open your app's Info.plist and add a row with a key called URL Types.
  2. Expand the URL Types item, and Item 0 under it and you'll see URL Identifier
  3. Enter your app's bundle identifier (e.g. com.myCompany.myApp) as the URL Identifier value.
  4. Add another row to Item 0 and enter URL Schemes.
  5. Expand the URL Schemes and under Item 0 type in the name for your custom scheme (e.g. myScheme).

You should now be able to open your app from Safari by typing myScheme:// in the address bar. Alternatively, from your app, you can launch the other app like this:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"myScheme://"]];

Note that you can also send parameters to the app you're launching with the url scheme (more on that here).

Gad
  • 2,867
  • 25
  • 35
  • Hi GAD, thanks for the comment. I will do this. thanks for help – Sagar Sep 17 '14 at 06:10
  • Hi GAD, I tried with your way. I added these fields. my URL schema is TestApp. then I go to Browser and type ://TestApp in address bar the phone didn't open the app. But app was already in device. Is I am doing anything wrong while typing it in address bar. – Sagar Sep 17 '14 at 06:42
  • You need to type TestApp:// is that what you typed? – Gad Sep 17 '14 at 09:40
  • 2
    Thanks, this works beautifully and is just what I needed. By the way, I have found that the URL Scheme name (e.g., "myScheme://") is case-insensitive, at least on my iPad Mini A1432 running iOS 8.3. – Blisterpeanuts Jun 12 '15 at 14:30
  • 1
    The URL works fine on Safari but not with Chrome or any other browsers.. Does anyone know how to do it with other browsers too.??? – Talib Shabbir Hussain Aug 17 '15 at 07:11
  • what if some one need to sennd some data from web with open applicaton? – Sultan Ali Jun 22 '20 at 10:17
  • Hi @Gad, if application is not installed than how do we navigate user to App Store ? – Sathish Gadde Sep 15 '22 at 04:05
13

With iOS9 Apple introduced a way to open installed app from Links. Here is the official link for this : Apple universal links

Praveen
  • 680
  • 5
  • 13
  • 2
    It's worth noting that Universal Links will not open with a link from the same domain. So for example say you're on https://example.com/page-1, if you click a link to https://example.com/page-2 you will not be able to get the app to open via Universal Links. – Jack Jun 24 '20 at 16:28
  • 1
    @Jack Then how would you do it then: Launching your app with the same domain as the webpage you are on? Is there something like an `intent:` launch point as with Android? – Torsten Engelbrecht May 19 '23 at 00:20