0

Any ideea how is this getting true in

appUrl = "http://dum:site2015@jobz.store.com/
if UIApplication.shared.canOpenURL(appUrl!){
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(appUrl!)
        }

and in url scheme i have jobz-com

the thing is it getting true althought I don't have the app installed ...instead is opening the url in safari... but why is not getting false since i don't have the app installed?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Mohamed Lee
  • 267
  • 1
  • 3
  • 18
  • 2
    don't use http for app url schemes – Arnab Apr 04 '18 at 08:55
  • The URL scheme used to to launch system apps through URLs. You shouldn't use it with https – M. Wojcik Apr 04 '18 at 08:57
  • @ArnabHore basically I've just hardcoded it here that url...but in normal scenario that url comes through api..and if I have the app installed I need to open..else open that link in Safari...but it always getting true in canOpenUrl – Mohamed Lee Apr 04 '18 at 08:57
  • Yes, api will send the url scheme in `jobz-com://` this format. Then you check that with `canOpenURL`, if it fails then open any website link... Other way is to use `Universal links` – Arnab Apr 04 '18 at 09:00

2 Answers2

1

This function does not check for apps installed. It just tells you if it can open that URL, in safari or through an app. A valid URL will always return true because the system can actually open it somewhere.

According to Apple's own documentation

A URL (Universal Resource Locator). At runtime, the system tests the URL’s scheme to determine if there is an installed app that is registered to handle the scheme. More than one app can be registered to handle a scheme. https://developer.apple.com/documentation/uikit/uiapplication/1622952-canopenurl

Safari is registered to handle any valid URL, so if the app using the scheme does not exist, the next application registered to read it is safari.

I don't think there's an open API for you to only open an URL if the app is installed.

And always make sure that your URL starts with the scheme you need and not HTTP/S. my-app://myurl/parameters

gmogames
  • 2,993
  • 1
  • 28
  • 40
  • ok..so basically what's the approach since a url with http /https comes through the api ..and I need to basically open the app if it s installed or open it in safari – Mohamed Lee Apr 04 '18 at 09:05
  • 1
    If you have the app installed and the app is registered to listen to your scheme, then the app will always open. If the app is not installed, the next best action is for safari to open. I would recommend you reading more about Universal Links: https://www.raywenderlich.com/128948/universal-links-make-connection – gmogames Apr 04 '18 at 09:09
  • With Universal Links, any URL from a specific domain will always open in the app instead of safari. You can even specify which URLs from the domain should open on the app or not. So www.yoursite.com/register can open your app and www.yousite.com/forgotPassword can open on web – gmogames Apr 04 '18 at 09:10
0

Do not use http:// or https:// to open apps. These are for websites. Use app url schemes like this:

jobz-com://

Edit

Another way of doing what the questioner is trying to achieve is to use Universal Link.

Here is the Apple's Official doc about Universal Links and you can follow this medium article which says:

The workaround approach to deep linking with URI schemes involves using a traditional http:// link to launch a web browser. This link contains a JavaScript redirect to a custom URI scheme, which is executed by the web browser to launch the app. If the redirect attempt fails because the app is not installed, the JavaScript then takes the user to the App Store or Play Store.

Instead of opening up Safari first when a link is clicked, iOS will check if a Universal Link has been registered (an AASA (apple-app-site-association) file should be there in the domain which contains the bundle id of the app and the paths the app should open) for the domain associated with the link, then check if the corresponding app is installed. If the app is currently installed, it will be opened. If it’s not, Safari will open and the http(s) link will load.

Arnab
  • 4,216
  • 2
  • 28
  • 50