iosswiftuisafarisafari-extensionopenurl

Force openURL to use safari SwiftUI for a Safari Extension


No doubt this has been asked before. However my search on SO has not returned any results so hence my having to ask.

I building a Safari Extension in my iOS App. This works in Safari obviously but there is no way to be sure Safari is the users default browser on their iOS device.

In my app I use @Environment(\.openURL) to open a website on which my Safari Extension will run. However that same openURL call will load the requested URL and launch whichever browser the user has selected to be their default. If its not Safari then my extension will not be available.

So how can I force openURL to use the Safari browser ignoring the users default browser choice? or better still how can I have my Safari Extension work in any browser on the users device?

My web search so far leads me to believe web extensions are not available on other mobile web browsers. Making safari the only choice. Hence why i need to try and force the openURL.

Thank you


Solution

  • You can force openURL to use Safari by using the Safari URL Scheme: x-safari-https://

    Replace http:// or https:// on the URL you want for x-safari-https://

    Example

    x-safari-https://www.stackoverflow.com
    

    It works both for iOS and macOS.