1

I want to get title of url from webView in react-native.

For example: I can get title of url from webView in iOS

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    titleLabel.text = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}

I did not find any solutions so far, so Is there anyone have some ideas?

Versus
  • 404
  • 4
  • 13

2 Answers2

5

I'm not sure whether this is the cleanest solution but you can inject some JavaScript into WebView to get some data, and then pass it back.

handleMessage(message) {
  console.log(message.nativeEvent.data);
}

render() {
  return (
    <WebView
      source={{ uri: "https://wonderloveapp.com" }}
      injectedJavaScript="window.postMessage(document.title)"
      onMessage={this.handleMessage}
    />
  )
}

References:

designorant
  • 2,222
  • 13
  • 24
0

You can use injectJavaScript or injectedJavaScript props to inject Javasctipt code to the URLs that you load and get title of the page. Take a look at this answer to learn how to get tile with javascript.

Then when you acquire title you can use onMessage prop to pass to react-native side.

bennygenel
  • 22,268
  • 6
  • 60
  • 76