KWebToNative is a dual-iOS and Android library that facilitates two-way communication between native applications and JavaScript apps running inside them. It also supports communication between iframes running inside a webview.
KWebToNative js will help to communicate web app to native both android and iOS
Include javascript file from js folder inside your head tag
<script type="text/javascript" src="KWebToNative.js"></script>
KWebNative allows to subscribe to event using the method on
where it takes the event name and subscribe to events that receive with eventname. It also allows more than one event subscription
KWebToNative.on(eventname, function(payload){
// here we get the payload send from native android or iOS
});
KWebNative allows to unsubscribe to event using the method off
where it takes the event name and unsubscribe all the events associated to the event.
KWebToNative.off(eventname);
KWebNative allows to send payload to Native android and iOS using the method send
KWebToNative.send(eventname,payload);
KWebToNative will help your iOS app to communicate with a Javascript application running inside WKWebview
- Copy the iOS folder to your project
- Add following pod to your pod file or call pod init and add following pod script
pod 'KWebToNative', :path => 'path to KWebToNative root'
- Call
Pod install
- Add
import KWebToNative
to your viewcontroller
KWebNative should be initialized with WKWebView instance
// Call under viewDidLoad
KWebToNative.shared.initialize(webView)
Add following snippet under WKNavigationDelegate method, Take a look at the sample for more information
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
guard let requestURL = navigationAction.request.url?.absoluteString else { return }
// Important to process the message from Web App
KWebToNative.shared.process(navigationAction.request.url)
decisionHandler(.allow)
}
KWebNative allows to subscribe to event using the method on
where it takes the event name and subscribe to events that receive with eventname. It also allows more than one event subscription
// Call under viewDidLoad
KWebToNative.shared.on(eventname, self)
extension YourViewController: KWebToNativeListenerDelegate{
func onMessage(_ payload: NSDictionary) {
// here you receive the payload
}
}
KWebNative allows to unsubscribe to event using the method off
where it takes the event name and unsubscribe all the events associated to the event.
KWebToNative.shared.off(eventname);
KWebNative allows to send payload from native iOS to webapp using the method send
do{
let payload = NSMutableDictionary()
try KWebToNative.shared.send(eventname,payload);
}
catch {
// here handle the error
}
KWebToNative will help your Android app to communicate with a Javascript application running inside WebView
- Copy kwebtonative-release.aar file to libs folder of your android project
- Add following imports
import com.kobil.kwebtonative.KWebToNative;
import com.kobil.kwebtonative.KWebToNativeListenerDelegate;
KWebNative should be initialized with WebView instance
// Call under viewDidLoad
KWebToNative.shared().initialize(webView, new WebViewClient())
KWebNative allows to subscribe to event using the method on
where it takes the event name and subscribe to events that receive with eventname. It also allows more than one event subscription
// Call under onStart()
KWebToNative.shared().on(eventname, new KWebToNativeListenerDelegate() {
@Override
public void onMessage(Map<Object, Object> payload) {
// here handle the payload
}
})
KWebNative allows to unsubscribe to event using the method off
where it takes the event name and unsubscribe all the events associated to the event.
KWebToNative.shared().off(eventname);
KWebNative allows to send payload from native iOS to webapp using the method send
HashMap<String, String> data = new HashMap<String, String>();
KWebToNative.shared().send(eventname, data);