Javascript error: "Unable to run user agent scripts because this document has previously accessed Apple Pay"

Hi All - Very recently, we've begun seeing this exception coming from multiple versions of our App:

Error Domain=WKErrorDomain Code=4 "A JavaScript exception occurred" UserInfo={WKJavaScriptExceptionLineNumber=0, WKJavaScriptExceptionMessage=Unable to run user agent scripts because this document has previously accessed Apple Pay. Documents can be prevented from accessing Apple Pay by adding a WKUserScript to the WKWebView's WKUserContentController., WKJavaScriptExceptionColumnNumber=0, NSLocalizedDescription=A JavaScript exception occurred}

we do not support Apple Pay on this site.

This particular part of our app interacts with a web based chart hosted on our domain, via several javascript functions.

we're using webView.evaluateJavascript(...)

Other parts of the app that interact with other pages do NOT encounter this issue.

As mentioned, this has worked perfectly fine in the past, and only recently started happening.

While I quadruple check things on my end, is there any documentation on this error and where I can begin troubleshooting this?

Thanks in advance.

Answered by kcatigbe_nbcu in 698751022

Just following up here with some closure - it appears to be that one of our 3rd party vendors silently enabled something that causes Apple Pay to be detected on the page. We've blocked this library on our end for WKWebview loads, and our pages are behaving as they were.

is there any documentation on this error and where I can begin troubleshooting this?

I am not aware of any documentation that specifically calls out this condition other than having it slightly touched on here in Bridging from Web-Based Interfaces. At any rate, you are correct to check you code again to make sure the Apple Pay on the Web APIs are not present, as these errors only show up in when these APIs are present and WKWebView is trying to execute JavaScript inside the web view context.

Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com

is there any documentation on this

I stand corrected, there is a WWDC video in 2020 that describes this behavior called What's new in Wallet and Apple Pay. Checkout around the 8:40 minute mark.

Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com

Thank you Matt, I will take this info to my team and review the WWDC video

Hi Matt - so digging a bit deeper, I'm seeing that adding:

let contentController = WKUserContentController()
contentController.addUserScript(...)

allows this all to work (makes sense, given the suggestion in the exception message)

We're still checking on our end how Apple Pay APIs would be available, as this code has been working untouched until recently. (we don't even have Apple Pay enabled for our domain - no merchant IDs, no environments setup)

As far as I understand it, it is functionally provided by the browser - but unsure how or why this has recently become an issue. (we're seeing 300k+ users affected, over 10M exceptions this month)

Accepted Answer

Just following up here with some closure - it appears to be that one of our 3rd party vendors silently enabled something that causes Apple Pay to be detected on the page. We've blocked this library on our end for WKWebview loads, and our pages are behaving as they were.

Javascript error: "Unable to run user agent scripts because this document has previously accessed Apple Pay"
 
 
Q