Enhancing WKWebView with "Open in xxx" Context Menu

Spent some hours researching swift, contextmenu, wkwebview, ios universal links, or sfsafariviewcontroller and crafted this guide on How can I add the "Open in xxx" context menu in WKWebView, similar to SFSafariViewController?. Hope it’s worth your time—feedback is welcome!

WKWebView Context Menu Enhancement

Everyone loves browsing seamlessly on their iPhones, right? Imagine you've whipped up an app using WKWebView for rendering web content. But if you're looking to jazz it up a bit with an "Open in xxx" context menu action, akin to what Apple's SFSafariViewController provides, then buckle up; this is your stop!

For iOS developers, crafting that cherished browsing experience spins around giving users robust control. This thrilling journey veers towards giving users the ability to quickly open links in external apps or browsers straight from your app's web view. Sounds exciting, doesn’t it?

The Main Question

The intrigue is about adding an "Open in xxx" context menu to WKWebView — pretty much like what you see in the bold and beautiful SFSafariViewController. The goal is to provide users the liberty of opening links in other applications directly from the context menu in WKWebView, almost as if they're in Safari.

Solutions to Consider

Let's stroll through some ways this can be achieved, discussed by vibrant minds in the community. The concepts here revolve around leveraging iOS Universal Links and creating custom context menus. Fasten your seatbelts!

1. Leveraging iOS Universal Links

Universal links are a great ally here. They essentially allow your app to open certain URLs straight in other apps, provided those apps are set up to handle such links. To set universal links, you will generally:

  • Create an apple-app-site-association file on your server.
  • Indicate the domains your app is associated with in Xcode.

Once set up, your app will be able to seamlessly hand off links to other apps that can open universal links. It's like handing over the baton in a relay race!

2. Crafting Custom Context Menus

Another exciting approach is to roll up your sleeves and create a custom context menu. You can override WKWebView's context menu by implementing WKUIDelegate methods.


    func webView(_ webView: WKWebView, contextMenuForElement elementInfo: WKContextMenuElementInfo, completionHandler: @escaping (UIContextMenuConfiguration?) -> Void) {
        let menuConfig = UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { _ in
            let openInSafari = UIAction(title: "Open in Safari", image: UIImage(systemName: "safari")) { _ in
                if let url = elementInfo.linkURL {
                    UIApplication.shared.open(url)
                }
            }
            return UIMenu(title: "", children: [openInSafari])
        }
        completionHandler(menuConfig)
    }
    

What we’ve done here is define a new context menu configuration, where we instructed the app to open links in Safari using the UIAction. Handy, isn’t it?

Personal Stories and Anecdotes

Here's a small aside - when I first attempted adding custom actions to WKWebView, I was astounded by how little customizability was allowed out of the box back in the day. The evolution of these classes is akin to the transition from silent modes to blaring recess bells in schools — stark and necessary! So, what’s your battle-hardened story of treading similar grounds in app development?

Wrapping Up

As we wrap up this discussion, remember that enhancing your WKWebView with features akin to SFSafariViewController fosters a user-friendly ecosystem wherein users can hop easily between apps. Leverage iOS Universal Links or roll up your sleeves with custom context menus to give your app that extra functionality and spark.

Would you dare dive into modifying your app with these tried and tested techniques? Go ahead, explore the possibilities and do share your experiences with the community. Happy coding!

Post a Comment

0 Comments