Compatibility Issue with WebKit.WKWebView.evaluateJavaScript(_:completionHandler:) in Xcode 16

I've encountered an issue after upgrading to Xcode 16. I have an overridden func of WebKit.WKWebView.evaluateJavaScript(_:completionHandler:), which no longer compiles in the new Xcode. I noticed that in Xcode 16, the completionHandler now has @MainActor and @Sendable annotations, which causes a compilation error.

public override func evaluateJavaScript(_ javaScriptString: String, completionHandler: (@MainActor @Sendable (Any?, (any Error)?) -> Void)? = nil)

When I add these annotations to my overridden method, it compiles fine in Xcode 16. However, this breaks compilation in older versions of Xcode. The documentation for this API doesn't explicitly mention any changes, but the behavior is clearly different between versions.

https://developer.apple.com/documentation/webkit/wkwebview/1415017-evaluatejavascript

Xcode 15:

Xcode 16:

What would be the best way to handle this situation? Should I use a compilation condition to differentiate between the versions? If so, what would be the correct condition to check? Was there a Swift compiler update in Xcode 16 that could be affecting this?

Or is it better to drop support for versions of Xcode earlier than 16?

Or I should not override that?

Thanks in advance for any insights!

Was there a Swift compiler update in Xcode 16 that could be affecting this?

I just posted some background into what's happening here for another thread that you may be interested in.

What would be the best way to handle this situation? Should I use a compilation condition to differentiate between the versions? [...] Or is it better to drop support for versions of Xcode earlier than 16?

However, one important thing is that thread's guidance is based on that developer being impacted through a closed source XCFramework that they ship, and how XCFrameworks and client version requirements in Xcode releases play together. Your situation here is different, as it sounds like this is your own code, and so you have the ability to recompile it with whatever Xcode version makes sense for you. Consider your broader needs — what's your plan for building your app with the iOS 18 SDK? If that's work you're actively performing, then you should make the changes here to update the method signature. If you're just exploring what work you may need to do for iOS 18, but are currently shipping your app with Xcode 15, then I'd only make the change here when you're ready to begin work for iOS 18. One common paradigm is separating your work by source code branch so that you can continue to build and ship the "current" version of your app, and doing work for a new iOS version on a branch, and thus that branch would require Xcode 16.

— Ed Ford,  DTS Engineer

We need to know how to make the code compatible with both Xcode 16 and earlier versions, as a group of people are working on the same branch but using different Xcode versions. Could you provide guidance on handling this?

The easiest answer here would be for all of your team to use the same Xcode version. Xcode 16 is released, so is there anything preventing from all of your team from using Xcode 16?

—Ed Ford,  DTS Engineer

There are a few complications with switching to Xcode 16 at the moment, so we’ll need to find another solution. We’ve introduced a flag like #if compiler(>=6) to handle both the new and old method signatures. Does this approach work for everyone?

#if compiler(>=6) {
public override func evaluateJavaScript(_ javaScriptString: String, completionHandler: (@MainActor @Sendable (Any?, (any Error)?) -> Void)? = nil) {
    super.evaluateJavaScript(javaScriptString, completionHandler: completionHandler)
}
#else
public override func evaluateJavaScript(_ javaScriptString: String, completionHandler: ((Any?, Error?) -> Void)? = nil) {
    super.evaluateJavaScript(javaScriptString, completionHandler: completionHandler)
}
#endif
Compatibility Issue with WebKit.WKWebView.evaluateJavaScript(_:completionHandler:) in Xcode 16
 
 
Q