npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@afa-ag/nativescript-webview-interface

v1.4.5

Published

Nativescript plugin for bi-directional communication between webView and android/ios.

Downloads

4

Readme

npm npm

Nativescript-WebView-Interface

Nativescript Plugin for bi-directional communication between WebView and Android/iOS

Installation

From the terminal, go to your app's root folder and execute:

tns plugin add nativescript-webview-interface

Once the plugin is installed, you need to copy plugin file for webView into your webView content folder. e.g

cp node_modules/nativescript-webview-interface/www/nativescript-webview-interface.js app/www/lib/

Usage

For a quick start, you can check this Demo App and Blog Post. If you are using this plugin with Angular 2, you can check this angular version of the demo app.

Inside Native App

Insert a web-view somewhere in your page.

<Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="pageLoaded">
....
<web-view id="webView"></web-view>
....
</Page>

Initialize WebViewInterface Plugin in your javascript file.

var webViewInterfaceModule = require('nativescript-webview-interface');
var oWebViewInterface;

function pageLoaded(args){
    page = args.object;
    setupWebViewInterface(page) 
}

// Initializes plugin with a webView
function setupWebViewInterface(page){
    var webView = page.getViewById('webView');
    oWebViewInterface = new webViewInterfaceModule.WebViewInterface(webView, '~/www/index.html');
}

Note: Please note in above example that, we have not set src in template and we have passed it in constructor of WebViewInterface. This is recommended way to use this plugin to avoid issue of communication from web-view to android not working sometimes on some devices.

Use any API Method of WebViewInterface Class

function handleEventFromWebView(){
    oWebViewInterface.on('anyEvent', function(eventData){
        // perform action on event
    });
}

function emitEventToWebView(){
    oWebViewInterface.emit('anyEvent', eventData);
}

function callJSFunction(){
    oWebViewInterface.callJSFunction('functionName', args, function(result){
        
    });
}

If you want to emit an event or call a JS function on load of the page, you need to call all such code once webView is loaded

webView.on('loadFinished', (args) => {
    if (!args.error) {
        // emit event to webView or call JS function of webView
    }
});

Inside WebView

Import nativescript-webview-interface.js in your html page.

<html>
    <head></head>
    <body>
        <script src="path/to/nativescript-webview-interface.js"></script>
        <script src="path/to/your-custom-script.js"></script>        
    </body>
</html>

Use any API Method of window.nsWebViewInterface inside webview

var oWebViewInterface = window.nsWebViewInterface;

// register listener for any event from native app
oWebViewInterface.on('anyEvent', function(eventData){
    
});

// emit event to native app
oWebViewInterface.emit('anyEvent', eventData);

// function which can be called by native app
window.functionCalledByNative = function(arg1, arg2){
    // do any processing
    return dataOrPromise;
}

API

Native App API

Constructor:

WebViewInterface(webView: WebView, src?: string)

webView is an instance of nativescript web-view.

src is the url/local path to be loaded in web-view. If it is set, then you don't need to set it in src attribute in xml file. For proper functioning of web-view to native communication on all device's it is recommended to set src here.

API Methods of WebViewInterface Class:

on(eventOrCmdName: string, callback: (eventData: any) => void): void

Use this method to assign listener to any event/command emitted from webView.

Callback will be executed with the data sent from webView in any format.

off(eventOrCmdName: string, callback?: (eventData: any) => void): void

Use this method to de-register listener of any event/command emitted from webView.

If callback is not set, all the event listeners for this event will be de-registered.

emit(eventOrCmdName: string, data: any): void

Use this method to emit any event/command from native app to webView with data in any format.

callJSFunction(webViewFunctionName: string, args: any[], successHandler: (result: any) => void, errorHandler: (error: any) => void): void

Use this method to call to any javascript function in global scope in webView.

Arguments are optional. But if supplied, must be in array format.

If the function is successfully executed, the successHandler will be called with the result returned by the JS Function. If promise is returned from the JS Function, the resolved value will come as result. If the function execution generates any error, the errorHandler will be called with the error.

destroy(): void

Use this method to clean up webviewInterface resources (eventListeners) to avoid memory leak.

WebView API

API Methods available in window.nsWebViewInterface global variable.

on(eventOrCmdName: string, callback: (eventData: any) => void): void

Use this method to assign listener to any event/command emited from native app.

Callback will be executed with the data sent from native app in any format.

emit(eventOrCmdName: string, data: any): void

Use this method to emit any event/command from webView to native app with data in any format.