5

I'm working on an ios react native app, I'm using webview to get data periodically, and use it in my app, but when I lock my iphone device, the webview stop sending data to react native.

onWebViewLoad() {
   setInterval(function(){
      this.refs.webview.injectJavaScript('window.ReactNativeWebView.postMessage(window.getData());');
   }, 10000);
}

handleMessage(event) {
   // receive data
   //doSomething(event.nativeEvent.data)
}
<WebView
  source={{ uri: this.state.myPage }}
  javaScriptEnabled={true}
  startInLoadingState={true}
  onLoad={this.onWebViewLoad}
  ref="webview"
  onMessage={this.handleMessage}
  />

Note: code is working fine when app in foreground or background, but when I open the app and lock the screen the webview stops sending data.

Is there a way to make the webview send data after screen lock.

Yeheshuah
  • 1,216
  • 1
  • 13
  • 28
El houcine bougarfaoui
  • 35,805
  • 9
  • 37
  • 37
  • According to this [post](https://stackoverflow.com/questions/7888490/how-can-i-detect-screen-lock-unlock-events-on-the-iphone/14271705#14271705) you can detect Lock or Unlock status of iPhone. I think you should call your method for both states. – Reza Dehnavi Jan 28 '20 at 03:12

1 Answers1

0

It should not be only the WebView which is blocked. I think also the timers are blocked when screen is locked.

Closely correlated with the subject called wake-lock for which - as mentioned in the article - sometimes playing hidden video methodology is used to avoid the CPU sleep.

Using the similar workaround, you can try the method in this github post to use the timers when screen is locked. (*)

Quoted from github post of the method:

...this seems to work for me:

  1. Start background timer in componentWillMount()

  2. Play a silent sound during the time when you want the background timer to be active. This can be something like react-native-sound file which is playing on repeat

  3. Add the following to your info.plist file:

<key>UIBackgroundModes</key>
<array>
 <string>audio</string>
</array>

Note that you should still abide by the apple terms of service, and only do the above if you are in one of their accepted use-cases for background playing.

(*) Please note, this requires using the react-native-background-timer and react-native-sound packages. Its logic is very similar to the above wake-lock article. And also it is better to think carefully if what you are doing complies with IOS policy or not.

Mehmet Kaplan
  • 1,723
  • 2
  • 20
  • 43
  • I'm already playing a video in background, and that problem still exist, I think the problem is related to the execution of javascript inside the webview it's get blocked when sceen lock like in safari browser – El houcine bougarfaoui Jan 24 '20 at 15:03
  • I know a silly question but let me ask, can the phone be in vibration mode? (Once my audio failed because of it in iOS) – Mehmet Kaplan Jan 24 '20 at 15:07
  • are you calling state changes after some callback or whatnot? I had a similar problem when I used setState after calling some other function, which made the app hang sometimes, and didn't have any feedback like a crash or something. – André Pinto Jan 27 '20 at 12:58