On Click of anchor (a) open a url in inappbrowser and show loading

I've a write some functions for this :



document.onclick = function(e) {
e = e || window.event;
var element = e.target || e.srcElement;

if (element.tagName == 'A' && ((element.href.indexOf('http://') >= || element.href.indexOf('https://') >= 0)) && element.href.indexOf('//localhost:') < 0 ) {
e.preventDefault();
var ref = window.open(element.href, "_blank", "location=yes");;
ref.addEventListener('loadstart', inAppBrowserbLoadStart);
ref.addEventListener('loadstop', inAppBrowserbLoadStop);
ref.addEventListener('loaderror', inAppBrowserbLoadError);
ref.addEventListener('exit', inAppBrowserbClose);
return false;
}
}

function inAppBrowserbLoadStart(event) {
   navigator.notification.activityStart("Please Wait", "It'll only take a moment...");
}

function inAppBrowserbLoadStop(event) {
   navigator.notification.activityStop();
}

function inAppBrowserbLoadError(event) {
   navigator.notification.activityStop();
}

function inAppBrowserbClose(event) {
   navigator.notification.activityStop();
   ref.removeEventListener('loadstart', inAppBrowserbLoadStart);
   ref.removeEventListener('loadstop', inAppBrowserbLoadStop);
   ref.removeEventListener('loaderror', inAppBrowserbLoadError);
   ref.removeEventListener('exit', inAppBrowserbClose);

}

Convert time form AM/PM to 24 Hours fromat

I've made a function for this, it'll return the array of hour and minute :


function am_pm_to_hours(time) {
    var hours = Number(time.match(/^(\d+)/)[1]);
var minutes = Number(time.match(/:(\d+)/)[1]);
var AMPM = time.match(/\s(.*)$/)[1];
if(AMPM == "PM" && hours<12) hours = hours+12;
if(AMPM == "AM" && hours==12) hours = hours-12;
var sHours = hours.toString();
var sMinutes = minutes.toString();
if(hours<10) sHours = "0" + sHours;
if(minutes<10) sMinutes = "0" + sMinutes;
//console.log(sHours + ":" + sMinutes);
    return [sHours, sMinutes];

}

Add icon in android lollipop push notification

Use this code :


PushNotification.init({
"android" : {
"senderID" : "292145204069",
"icon" : "notification_icon", // It will search for notification_icon.png in your android drawable folder
"iconColor": "#0076cb"
}

});

Play a video in iframe


Write the following code :

<iframe width="100%" height="auto" ng-if="video_url" onerror="this.style.display = 'none'" src="{{video_url | VideoUrl}}"></iframe> 
.filter('VideoUrl', ['$sce', function($sce) {
    return function(val) {
        return $sce.trustAsResourceUrl(val);
    };
}])

Show loader on InAppBrowser while loading

For InAppBrowser You've to install the plugin 'cordova-plugin-inappbrowser' :

cordova plugin add cordova-plugin-inappbrowser

For Dialogs You've to install the plugin 'cordova-plugin-dialogs' :

cordova plugin add cordova-plugin-dialogs
Then write this code for open inappbrowser :


var ref = window.open(encodeURI('http://www.google.com', '_blank', 'location=yes');

ref.addEventListener('loadstart', inAppBrowserbLoadStart);
ref.addEventListener('loadstop', inAppBrowserbLoadStop);
ref.addEventListener('loaderror', inAppBrowserbLoadError);
ref.addEventListener('exit', inAppBrowserbClose);


function inAppBrowserbLoadStart(event) {
   navigator.notification.activityStart("Please Wait", "It'll only take a moment...");
}

function inAppBrowserbLoadStop(event) {
   navigator.notification.activityStop();
}

function inAppBrowserbLoadError(event) {
   navigator.notification.activityStop();
}

function inAppBrowserbClose(event) {
   navigator.notification.activityStop();
   ref.removeEventListener('loadstart', inAppBrowserbLoadStart);
   ref.removeEventListener('loadstop', inAppBrowserbLoadStop);
   ref.removeEventListener('loaderror', inAppBrowserbLoadError);
   ref.removeEventListener('exit', inAppBrowserbClose);
}



Check GPS state (on/off) in ionic/cordova

Fist you've to install the plugin https://github.com/dpa99c/cordova-diagnostic-plugin then simply execute this code after platform ready :

Check GPS state (on/off) in ionic/cordova

cordova.plugins.diagnostic.isLocationEnabled(function(enabled){
    console.log("Location is " + (enabled ? "enabled" : "disabled"));
}, function(error){
    console.error("The following error occurred: "+error);
});


Check WiFi is enable or not?

cordova.plugins.diagnostic.isWifiEnabled(function(enabled){
    console.log("WiFi is " + (enabled ? "enabled" : "disabled"));
}, function(error){
    console.error("The following error occurred: "+error);
});

Check device has a camera AND the application is authorized to use it. or not?

cordova.plugins.diagnostic.isCameraEnabled(function(exists){
    console.log("Device " + (exists ? "does" : "does not") + " have a camera");
}, function(error){
    console.error("The following error occurred: "+error);
});

Enables/disables WiFi on the device

cordova.plugins.diagnostic.setWifiState(function(){
    console.log("Wifi was enabled");
}, function(error){
    console.error("The following error occurred: "+error);
},
true);

Checks the device has Bluetooth capabilities and if it has then Bluetooth is on or off?

cordova.plugins.diagnostic.isBluetoothEnabled(function(enabled){
    console.log("Bluetooth is " + (enabled ? "enabled" : "disabled"));
}, function(error){
    console.error("The following error occurred: "+error);
});


Enables/disables Bluetooth on the device

cordova.plugins.diagnostic.setBluetoothState(function(){
    console.log("Bluetooth was enabled");
}, function(error){
    console.error("The following error occurred: "+error);
},
true);

Publish Ionic App on Play Store in Ionic/Cordova


When You want to publish your app on play store, First thing you have to do is 'create a keystore file'
(Make sure, when you've published app using a keystore then for publish this particular app next version, you must need the same keystore file, without will not able to publish the new version of this app)

1.) Create a keystore file by cmd :

keytool -genkey -v -keystore keystore_name.keystore -alias your_app_alias -keyalg RSA -keysize 2048 -validity 10000

After this you've to answer some question and then you keysore file will created in root folder of your   project.

2.) To sign the unsigned APK, run the jarsigner tool which is also included in the JDK:


jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore keystore_name.keystore android-release-unsigned.apk alias_name

3.) Finally, you need to run the zip align tool to optimise the APK file :

First you to go into the zipalign folder, eg.:
    For Windows :
             cd /path/to/Android/sdk/build-tools/VERSION
             zipalign -v 4 android-release-unsigned.apk android_store_apk.apk

    For OS X :
             cd Users/UserName/Library/Android/sdk/build-tools/VERSION
             ./zipalign -v 4 android-release-unsigned.apk android_store_apk.apk


Now You can Publish android_store_apk.apk on the play store.