How to selectively open other apps on iOS using UIApplication.shared.open

700 Views Asked by At

I have an app that allows users to open URLs. These can be handled via a SafariView, but if an app has registered to handle the URL, I'd like to use:

    if UIApplication.shared.canOpenURL(url) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    }

However, this seems to fallback on opening Safari. I'd like to fallback on just using SafariView inside my own app for this case. Is it possible to selectively choose which external apps are used?

1

There are 1 best solutions below

0
On

The "universalLinksOnly" option can be used along with checking the completion handler for true.

    if UIApplication.shared.canOpenURL(url) {
        UIApplication.shared.open(url, options: [.universalLinksOnly: true], completionHandler: { wasHandled in
            if !wasHandled {
                handleExternalUrlWithSafariView(url)
            }
        })
    } else {
        handleExternalUrlWithSafariView(url)
    }

Where "handleExternalUrlWithSafariView" is a function I define to open a SafariView with the given URL.