Search code examples
iosswifturl-schemeuiapplication

Not fully understanding UIApplication.shared.canOpenUrl


Any ideea how is this getting true in

appUrl = "http://dum:[email protected]/
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?


Solution

  • 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