Issue with Copy Functionality in WKWebView Using Mac Catalyst

Hello,

I am developing a Mac application via Mac Catalyst and encountering an issue with WKWebView. Specifically, I'm loading a webpage (e.g., https://translate.google.com) in WKWebView, but when I press the copy button on the page, the content doesn't actually copy to the clipboard.

I've attempted modifying the UserAgent without any success. Here is the relevant part of my code:

override func viewDidLoad() {
    super.viewDidLoad()

    let config = WKWebViewConfiguration()
    config.preferences = WKPreferences()
    config.defaultWebpagePreferences.preferredContentMode = .desktop
    
    let webView = WKWebView(frame: .zero, configuration: config)
    webView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(webView)
    webView.scrollView.showsVerticalScrollIndicator = false
    webView.backgroundColor = UIColor.white
    webView.scrollView.backgroundColor = UIColor.white
    
    webView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    webView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    webView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    webView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

    if let url = URL(string: "https://translate.google.com") {
        let req = URLRequest(url: url)
        webView.load(req)
    }
}

This issue occurs on many websites, not just the one listed. However, the copy functionality is available on some URLs.

I've attempted modifying the UserAgent without any success.

webView.customUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15"

What content specifically are you trying to copy to clipboard?

Rico

WWDR - DTS - Software Engineer

@Engineer

The content I am trying to copy is text.

For example, when using Google Translate (https://translate.google.com) within WKWebView, I translate a piece of text and then click the web page’s copy button. Although the webpage indicates that the copying was successful, there is actually no content copied to the clipboard.

This functionality works as expected on both iPhone and iPad; however, it fails when running through Mac Catalyst.

Issue with Copy Functionality in WKWebView Using Mac Catalyst
 
 
Q