GithubHelp home page GithubHelp logo

alexxnica / react-native-wkwebview Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cralpha/react-native-wkwebview

0.0 1.0 0.0 181 KB

WKWebview Component for React Native

License: MIT License

JavaScript 33.38% Objective-C 65.09% Ruby 1.53%

react-native-wkwebview's Introduction

WKWebView Component for React Native

npm version

React Native comes with WebView component, which uses UIWebView on iOS. This component uses WKWebView introduced in iOS 8 with all the performance boost. Deployment Target >= iOS 8.0 is required (which is React Native's current minimum deployment target anyway).

Install

Alternative #1

  1. Install from npm (note the postfix in the package name): npm install react-native-wkwebview-reborn
  2. In the XCode's "Project navigator", right click on your project's Libraries folder ➜ Add Files to <...>
  3. Go to node_modules ➜ react-native-wkwebview ➜ ios ➜ select RCTWKWebView.xcodeproj
  4. Go your build target ➜ Build Phases ➜ Link Binary With Libraries, click "+" and select libRCTWkWebView.a (see the following screenshot for reference) Linking
  5. Compile and profit (Remember to set Minimum Deployment Target = 8.0)

Alternative #2

  1. Install from npm (note the postfix in the package name): npm install react-native-wkwebview-reborn
  2. run rnpm link

Usage

import WKWebView from 'react-native-wkwebview-reborn';

Try replacing your existing WebView with WKWebView and it should work in most cases.

If your React Native < 0.40, please user 0.x.x versions.

Compatibility with UIWebView

WKWebView aims to be a drop-in replacement for UIWebView. However, some legacy UIWebView properties are not supported.

Additional props:

  • onProgress

A callback to get the loading progress of WKWebView. Derived from estimatedProgress property.

<WKWebView onProgress={(progress) => console.log(progress)} />

progress is a double between 0 and 1.

  • openNewWindowInWebView

If set to true, links with target="_blank" or window.open will be opened in the current webview, not in Safari. Default is false.

  • sendCookies

Set sendCookies to true to copy cookies from sharedHTTPCookieStorage when calling loadRequest. This emulates the behavior of react-native's WebView component. You can set cookies using react-native-cookies Default is false.

  • source={{file: '', allowingReadAccessToURL: '' }}

This allows WKWebView loads a local HTML file. Please note the underlying API is only introduced in iOS 9+. So in iOS 8, it will simple ignores these two properties. It allows you to provide a fallback URL for iOS 8 users.

<WKWebView source={{ file: RNFS.MainBundlePath + '/data/index.html', allowingReadAccessToURL: RNFS.MainBundlePath }} />
  • userAgent="MyUserAgent" (or customUserAgent="...")

Set a custom user agent for WKWebView. Note this only works on iOS 9+. Previous version will simply ignore this props.

  • hideKeyboardAccessoryView

This will hide the keyboard accesory view (< > and Done). Default is false.

Communication from WKWebview to React Native

  • onMessage

This utilizes the message handlers in WKWebView and allows you to post message from webview to React Native. For example:

<WKWebView onMessage={(e) => console.log(e)} />

Then in your webview, you can post message to React Native using

window.webkit.messageHandlers.reactNative.postMessage({data: 'hello!'});

Then your React Native should have

{name: 'reactNative', body: {data: 'hello!'}}

The data serialization flow is as follows:

 JS — (via WKWebView) --> ObjC --- (via React Native Bridge) ---> JS

So I recommend to keep your data simple and JSON-friendly.

Communication from React Native to WkWebView

There is a evaluateJavaScript method on WKWebView, which does exactly what its name suggests. To send message from React Native to WebView, you can define a callback method on your WebView:

window.receivedMessageFromReactNative = function(data) {
  // Code here
  console.log(data);
}

Then you can send message from React Native with this method call:

// <WKWebView ref="webview" />
this.refs.webview.evaluateJavaScript('receivedMessageFromReactNative("Hello from the other side.")');

Currently supported props are:

  • automaticallyAdjustContentInsets
  • contentInset
  • html (deprecated)
  • injectedJavaScript
  • onError
  • onLoad
  • onLoadEnd
  • onLoadStart
  • onNavigationStateChange
  • renderError
  • renderLoading
  • source
  • startInLoadingState
  • style
  • url (deprecated)
  • bounces
  • onShouldStartLoadWithRequest
  • pagingEnabled
  • scrollEnabled
  • directionalLockEnabled

Unsupported props are:

  • mediaPlaybackRequiresUserAction
  • scalesPageToFit
  • domStorageEnabled
  • javaScriptEnabled
  • allowsInlineMediaPlayback
  • decelerationRate

If you look at the source, the JavaScript side is mostly derived from React Native's WebView. The Objective C side mostly deals with the API difference between UIWebView and WKWebView.

Contribute

We battle test this component against our app. However, we haven't use all the props so if something does not work as expected, please open an issue or PR.

react-native-wkwebview's People

Contributors

anback avatar autopulated avatar diegum avatar gameboyvito avatar headlessme avatar idris avatar insraq avatar mikelambert avatar nihgwu avatar oblador avatar okadan-bp avatar pokute avatar sportnak avatar srandazzo avatar tomasc avatar williamrijksen avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.