Skip to content

Instantly share code, notes, and snippets.

@marchbold
Last active September 21, 2021 00:56
Show Gist options
  • Save marchbold/c052b5ff35f6e2c3a090 to your computer and use it in GitHub Desktop.
Save marchbold/c052b5ff35f6e2c3a090 to your computer and use it in GitHub Desktop.
AirBridge Javascript helper to communicate from JS to the WebView instance
/**
*
*/
var AirBridge = (function() {
var instance;
function createInstance() {
var object = new Object();
object.useWindowLocation = true;
return object;
}
return {
setUseWindowLocation: function( $shouldUseWindowLocation ) {
if (!instance) {
instance = createInstance();
}
instance.useWindowLocation = $shouldUseWindowLocation;
},
message: function( $message ) {
if (!instance) {
instance = createInstance();
}
if (!instance.useWindowLocation) {
NativeWebView.airBridge( $message );
}
else {
window.location = "airBridge:" + $message;
}
}
};
})();
// com.distriqt.NativeWebView
@marchbold
Copy link
Author

@mesaglam
Copy link

mesaglam commented Dec 6, 2017

It gives error like

net::ERR_UNKNOWN_URL_SCHEME

in android devices. You should replace;

if (!instance.useWindowLocation) {
NativeWebView.airBridge( $message );
}
else {
window.location = "airBridge:" + $message;
}

with;

if (!instance.useWindowLocation) {
NativeWebView.airBridge( $message );
}
else {
if(sys == 'iOS')
{
location = "airBridge:" + $message;
}else{
location = "http://airBridge:" + $message;
}
}

I've tested both iOs and Android. It works well...

@mesaglam
Copy link

mesaglam commented Dec 6, 2017

Need this function maybe:


_function getMobileOperatingSystem() {
  var userAgent = navigator.userAgent || navigator.vendor || window.opera;

    if (/windows phone/i.test(userAgent)) {
        return "Windows Phone";
    }

    if (/android/i.test(userAgent)) {
        return "Android";
    }

    // iOS detection from: http://stackoverflow.com/a/9039885/177710
    if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
        return "iOS";
    }

    return "unknown";
}

var sys = getMobileOperatingSystem();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment