Search code examples
iosswiftwkwebview

WKContentRuleListStore.default() crash in iOS 14


My app makes use of content blocking using WKContentRuleListStore. However, when I try to compile using Swift 5.3/iOS 14, I get a runtime crash when trying to instantiate WKContentRuleListStore.default().

I did not notice this crash in iOS 13, or even building with the iOS 13 SDK and running on iOS 14.

The error presented is only EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0) at callsite.

The stack-trace:

#0  0x00007fff28541233 in WTFCrashWithInfo(int, char const*, char const*, int) ()
#1  0x00007fff286c6868 in WebKit::runInitializationCode(void*) ()
#2  0x00007fff202cbf2a in std::__1::__call_once(unsigned long volatile&, void*, void (*)(void*)) ()
#3  0x00007fff286c3623 in WebKit::InitializeWebKit2() ()
#4  0x00007fff288e23ea in API::ContentRuleListStore::ContentRuleListStore(WTF::String const&, bool) ()
#5  0x00007fff288e2348 in API::ContentRuleListStore::ContentRuleListStore(bool) ()
#6  0x00007fff288e23ac in API::ContentRuleListStore::nonLegacyDefaultStore() ()
#7  0x00007fff2876013d in +[WKContentRuleListStore defaultStore] ()
#8  0x000000010c797fa9 in static WebViewRules.compile() at /MyApp/Features/WebView/WebViewRules.swift:36

Solution

  • Found the solution pretty quickly but wanted to document it for any folks that run into the same issue.

    WKContentRuleListStore.default() must be called on the main thread. It appears this is a new (undocumented) requirement introduced in iOS 14 and the corresponding SDK.

    DispatchQueue.main.async {
      let store = WKContentRuleListStore.default()
      // use the store...
    }