question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

White screen on iOS + injectJavaScript does not work in that state

See original GitHub issue

Bug description:

When we leave our app in the background mode on iOS for a white and then bring it back to the active state, the webview fails to render the content and renders only a white screen. This issue is non-deterministic, happens only from time to time and not on all devices.

We found out that restarting the app or calling reload() method of the webview resolves the issue. The main problem is how to detect this state. We figured out that when a blank screen is shown, injectJavaScript does not work at all, i.e. even injectJavaScript('alert("Test");') does not work (alert is not shown).

So we are currently trying to detect this state by sending a message from within the webview when the app is taken back to the ‘active’ state by calling injectJavaScript('window.ReactNativeWebView.postMessage("app.healthCheck");'). We set a 1s timeout and if this message is not received by the onMessage handler, we reload the webview.

Is there a better way to fix this issue?

To Reproduce:

It’s hard to reproduce, completely non-deterministic. Happens only from time to time on iOS.

Expected behavior:

The app is in the same state as it was before bringing it to the background state, i.e. webview paints the content of the current URL, not a blank page.

Environment:

  • OS: iOS
  • OS version: 15.2
  • react-native version: 17.0.2
  • react-native-webview version: 11.15.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:14

github_iconTop GitHub Comments

3reactions
skvariljancommented, Feb 1, 2022

@dgbelidji This is a minimum working example:

import React, { useRef } from 'react'
import { WebView } from 'react-native-webview'

const App = () => {
    const webViewRef = useRef()

    return (
        <WebView
            source={{ uri: 'https://www.google.com/' }}
            onContentProcessDidTerminate={() => webViewRef.current.reload()}
            ref={webViewRef}
        />
    )
}
3reactions
emin93commented, Dec 28, 2021

We had the same issue and fixed it by handling onContentProcessDidTerminate and re-mounting the WebView when the app becomes active again.

https://github.com/react-native-webview/react-native-webview/blob/master/docs/Reference.md#oncontentprocessdidterminate

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native Webview - injectJavascript not working
Hi, how do i pass a value from my react native app to my webview. eg. i have a session id created when...
Read more >
React Native WebView: A complete guide - LogRocket Blog
The injectJavaScript method. Below, you can find the final result of this step-by-step guide. As you can see, we display random programming ...
Read more >
react native webview background color - You.com | The AI ...
Issue Description. I have a screen which contain a WebView component it's works fine but if I pop screen then the problem shows...
Read more >
WebView - Android Developers
This method was deprecated in API level 18. Use WebView.loadUrl("about:blank") to reliably reset the view state and release page resources (including any ...
Read more >
“white screen after splash screen (ios) reaact native” Code ...
Soluction of white screen issue in react-native-splash-screen // Provided by Youtube: CodingBite // Add this in your styles.xml file.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found