Skip to content

Commit 8dc907a

Browse files
author
Tyschenko
committed
fix: Website niftyisland.com doesn't open on iOS, crashing the webview. Remove Service workers from all websites on iOS in the WkWebView to prevent the crash
1 parent d0cccc5 commit 8dc907a

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

apple/RNCWebViewImpl.m

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,6 +1853,45 @@ - (void)resetupScripts:(WKWebViewConfiguration *)wkWebViewConfig {
18531853
WKUserScript *userScript = [[WKUserScript alloc] initWithSource:html5HistoryAPIShimSource injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
18541854
[wkWebViewConfig.userContentController addUserScript:userScript];
18551855

1856+
NSString *serviceWorkerPreventCrashesSource = @"(function() {\n"
1857+
" if (!('serviceWorker' in navigator)) {\n"
1858+
" Object.defineProperty(navigator, 'serviceWorker', {\n"
1859+
" value: {\n"
1860+
" register: function(scriptURL, options) {\n"
1861+
" return Promise.resolve({\n"
1862+
" installing: null,\n"
1863+
" waiting: null,\n"
1864+
" active: {\n"
1865+
" scriptURL: scriptURL,\n"
1866+
" state: 'activated'\n"
1867+
" },\n"
1868+
" scope: options?.scope || '/',\n"
1869+
" update: () => Promise.resolve(),\n"
1870+
" unregister: () => Promise.resolve(true)\n"
1871+
" });\n"
1872+
" },\n"
1873+
" getRegistration: () => Promise.resolve(null),\n"
1874+
" getRegistrations: () => Promise.resolve([]),\n"
1875+
" ready: Promise.resolve({\n"
1876+
" installing: null,\n"
1877+
" waiting: null,\n"
1878+
" active: null,\n"
1879+
" scope: '/'\n"
1880+
" }),\n"
1881+
" controller: null,\n"
1882+
" addEventListener() {},\n"
1883+
" removeEventListener() {}\n"
1884+
" },\n"
1885+
" configurable: true\n"
1886+
" });\n"
1887+
" }\n"
1888+
"})();";
1889+
WKUserScript *serviceWorkerPreventCrashesScript = [[WKUserScript alloc]
1890+
initWithSource:serviceWorkerPreventCrashesSource
1891+
injectionTime:WKUserScriptInjectionTimeAtDocumentStart
1892+
forMainFrameOnly:NO];
1893+
[wkWebViewConfig.userContentController addUserScript:serviceWorkerPreventCrashesScript];
1894+
18561895
if(_sharedCookiesEnabled) {
18571896
// More info to sending cookies with WKWebView
18581897
// https://stackoverflow.com/questions/26573137/can-i-set-the-cookies-to-be-used-by-a-wkwebview/26577303#26577303

0 commit comments

Comments
 (0)