Check if device's running cordova - android

I have ready web application and i've made an android app ,using Cordova, from it, it works good, but when i do this:
if (!(<any>window).cordova) {
return this.afAuth.signInWithPopup(provider)
.then(res => {
this.ngZone.run(() => {
this.router.navigate(['main-layout']);
});
}).catch(error => {
window.alert(error);
});
} else {
return this.afAuth.signInWithRedirect(provider);
}
I expect that on android app else will be reached, but it did not.
So, how can i check whether app is running on android (cordova) or on desktop in Angular?
I've tried this How to detect if I am in browser (local development) in Ionic 2, but it did not help me.
I'm using cordova 9, angular 10.
Thanks.

https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-device/index.html
This is a reference for getting device platform for cordova, it could be adding using cmd:
cordova plugin add cordova-plugin-device
Properties:
device.cordova
device.model
device.platform
device.uuid
device.version
device.manufacturer
device.isVirtual
device.serial
You could use device.platform to achieve what you need for example:
if (device.platform === 'iOS') {}
if (device.platform === 'browser') {}
if (device.platform === 'Android') {}
And tou could see full documentation in link above to see more things or other supported platforms

Related

Ionic3-Cordova-UWP : Ionic Storage doesn’t work when app is running

I have a problem with Ionic storage in my Ionic App for Windows (UWP app). I already tested and deployed my app for Android without any trouble.
Now I would like to generate an UWP app.
The app I’m developing is a workshop’s dashboard. The indicators change every day and I need to save them from one day to another. To do this I usethe storage module of Ionic.
When launching the command
$ionic serve
the app responds exactly as it should in the web browser (Firefox). From one launch to another my data are still there.
When I use
$ionic cordova run windows
the installation and launch steps of my app work fine. Every function of my app runs fine, but the call to storage (get and set) doesn’t work : when I close, and then reopen it as an autonomous application, I loose all data every time. It is the same when I run the app from Visual Studio.
Moreover, I write some console.log in the result of the promise and none of them is written in the Javascript console. It is like Windows does not recognize the command.
I tried with Visual Studio 2015 Update 3 and Visual Studio 2017, the result is identical. I followed the recommendation of Ionic Doc and add the target platform windows10 in the config.xml.
Does anyone have an idea of what I’m doing wrong ? Is there an incompatibility between Windows and Ionic ?
Thanks for any help.
Config :
Windows 10 64-bits
Ionic CLI: 3.19.1
Cordova CLI : 8.0.0
Node : v6.11.2
Visual Studio Community 2017 (15.5.27130.2036)
Extract of my code maPage.ts :
import { Storage } from '#ionic/storage';
export class maPage {
constructor(public storage: Storage) {
this.downloadData();
}
ionViewWillLeave()
{
this.saveData();
}
TabData = {
id: 0,
label: '',
Tab1: [],
Tab2: [],
attri1 : 0,
attri2: false
}
Param = {
Objet1: {NbObj1: 7},
Objet2: {NbObj2: 5}
}
saveData()
{
var Data: object;
Data =
{
TData: this.TabData,
TParam: this.Param
}
this.storage.set('Data', Data).then(_=> {
console.log('Backup done!');
}, error => {
console.log('erreur : ', JSON.stringify(error))
});
}
downloadData()
{
this.storage.ready().then(()=>{
console.log('storage ready');
this.storage.get('Data').then((val) => {
this.TabData = val.TData;
this.Param = val.TParam;
console.log('Récupération terminée !');
}).catch(erreur => {
console.log('La variable Data est vide ou n\'existe pas!');
console.log('erreur : ', JSON.stringify(erreur));
}).catch(err => {
console.log('storage not ready');
console.log('err : ', JSON.stringify(err));
});
}
}
On Windows platform, console.log is not supported. To do that, you should install cordova-plugin-console, or need to use alert function.
Regarding storage, I am suggesting you could use localstorage or cordova-sqlite-storage.

geolocation does not work ionic 3 - android

I have problem with plugin
import { Geolocation } from '#ionic-native/geolocation';
The geolocation does not work ionic 3 - ANDROID but in web browser from my computer its normal for me, this is my code:
export class TestPage {
state: string = "";
constructor(private geolocation: Geolocation,
public platform: Platform) {
this.platform.ready().then(() => {
this.geolocation.getCurrentPosition().then((resp) => {
// resp.coords.latitude
// resp.coords.longitude
this.state+="-"+resp.coords.latitude;
}).catch((error) => {
this.state+="-"+error.message;
});
});
}
}
The result it´s above, I need your help, whats is the wrong??
You need to use below CLI with Android device.It won't work with --livereload.
Reason: Google regards localhost as secure, but others as not.
ionic cordova run android --prod --device
You can read more about this issue here and here.

How to debug native cordova plugins on ionic app?

I created a simple app with possibility to select photo from mobile phone using #ionic-native/photo-library. Now I want to debug it, print some console logs and check if photo is loaded properly:
private selectPhoto() {
this.photoLibrary.requestAuthorization().then(() => {
this.photoLibrary.getLibrary().subscribe({
next: library => {
library.forEach((libraryItem) => {
console.log(libraryItem) // i want to print it
})
},
error: err => {},
complete: () => { console.log('could not get photos'); }
})
})
.catch(err => console.log(err));
}
Unfortunatelly, I receive an error:
Native: tried calling PhotoLibrary.requestAuthorization, but Cordova is not available. Make sure to include cordova.js or run in a device/simulator
I read that native plugins must be tested on device but how to get output from code in my console? When I install apk I do not have any debug information.
Regards
The error means that the plugin won't properly work unless cordova.js is already loaded. This happens because cordova.js is the one responsible for loading your plugins, so if you are calling any of it before cordova is ready it just won't work.
You should use either:
document.addEventListener('deviceready', DeviceReady, false);
function DeviceReady() {
//your plugin code here...
}
or
$ionicPlatform.ready(function() {
//your plugin code here...
});
To properly debug an Android/iOS build with the help of a browser console (chrome/safari) make sure that you're building a debug apk/ipa with the option --debug at the CLI:
$ionic build android/ios --debug
I hope this may help you out =)

AdMob is not defined on Meteor app

I have a Meteor(v1.0.3.1) app and installed AdMob Cordova plugin like this:
meteor add cordova:com.google.cordova.admob#https://github.com/floatinghotpot/cordova-admob-pro/tarball/94a31660d1bb35337e3430e2608b7710ea4d882a
with the following code as the doc suggests:
if(Meteor.isCordova){
admobid = {
banner: 'ca-app-pub-xxxxxxxxxxxxxxxxxxx'
};
if(AdMob){
AdMob.createBanner( {
adId: admobid.banner,
position: AdMob.AD_POSITION.BOTTOM_CENTER,
autoShow: true
});
}
}
And started the app with meteor run android-device with my Android phone plugged in.
The following error appears in the stack trace:
Uncaught ReferenceError: AdMob is not defined
I looked for AdMob object on window and other places but couldn't find it.
I can confirm that the plugin you are using is working. But you should probably not use the tarball. Use the current version (2.7.4) instead.
Try this. This worked for me using the iOS simulator.
Remove the plugin you installed with meteor remove
Install with version number
meteor add cordova:com.google.cordova.admob#2.7.4
Run the simluator (use the Android one, if you do not have Xcode )
meteor run ios
Then the AdMob variable should be available. At least it was when I tried it.
Update
The plugin also works with Android.
`meteor run android`
Also, make sure that your code is in Meteor.startup(), to ensure that the Cordova plugin is available.
Meteor.startup(function () {
if (Meteor.isCordova) {
if (AdMob) {
AdMob.createBanner( {
adId: 'ca-app-pub-3080070244198226/2109901818',
position: AdMob.AD_POSITION.BOTTOM_CENTER,
isTesting: true,
autoShow: true,
success: function() {
console.log("Received ad");
},
error: function() {
console.log("No ad received");
}
});
} else {
console.log("No Admob");
}
} else {
console.log("No Cordova ");
}
}
I would suggest you to try with this other plugin:
meteor add cordova:com.admob.google#https://github.com/appfeel/admob-google-cordova/tarball/f3851132148aae4c600563d4124cc875c8c5f73e
Then in your code:
if(Meteor.isCordova){
if(admob){
admob.createBannerView({publisherId: "ca-app-pub-xxxxxxxxxxxxxxxxxxx"});
}
}
Meteor had a security upgrade v1.0.4. You need to add the regex for the website to your mobile config file to get the google sdk. Otherwise if you dont you will see the whitelist error in x-code's logs.
I used this to get the sdk.
App.accessRule('*://googleads.g.doubleclick.net');
reference from meteor telling you about the upgrade.
https://www.meteor.com/blog/2015/03/17/meteor-104-mongo-cordova-template-subscriptions

How to get the device UUID in ionic framework

installed cordova device plugin by :
sudo cordova plugin add org.apache.cordova.device
then downloaded ngCordova and included ng-cordova.min.js in to js folder and also included in index.html
next what i did is injected ngCordova as follows
angular.module('starter', ['ionic', 'starter.controllers','ngCordova'])
then included in controller as follows
angular.module('starter.controllers', [])
.controller('AppCtrl', function($scope, $ionicModal, $timeout, $ionicPlatform,$cordovaDevice)
but still getting the following errors
ReferenceError: device is not defined
at Object.getUUID (http://localhost:8100/js/ng-cordova.min.js:1:14929)
at new <anonymous> (http://localhost:8100/js/controllers.js:27:26)
at invoke (http://localhost:8100/lib/ionic/js/ionic.bundle.js:11591:17)
at Object.instantiate (http://localhost:8100/lib/ionic/js/ionic.bundle.js:11602:23)
at http://localhost:8100/lib/ionic/js/ionic.bundle.js:14906:28
at updateView (http://localhost:8100/lib/ionic/js/ionic.bundle.js:42986:30)
at eventHook (http://localhost:8100/lib/ionic/js/ionic.bundle.js:42933:17)
at Scope.$broadcast (http://localhost:8100/lib/ionic/js/ionic.bundle.js:20605:28)
at $state.transition.resolved.then.$state.transition (http://localhost:8100/lib/ionic/js/ionic.bundle.js:34122:22)
at wrappedCallback (http://localhost:8100/lib/ionic/js/ionic.bundle.js:19197:81)
Can you now tell me what went wrong?
If is there another way to read the Device UUID show me the direction to it.
Yes, there is another way. You just don't need the ngCordova for this.
When you add the plugin cordova plugin add org.apache.cordova.device it's loaded to your application and therefore the info you want is at window.device.
If you want to get device uuid at anywhere in the code you just need to call window.device.uuid.
If you want as soon as the app starts, then use:
ionic.Platform.ready(function(){
console.log( window.device.uuid );
});
If you are using '> ionic serve', device will be "not defined." Try in an emulator or physical device.
Use ngCordova and cordova Device plugin:
cordova plugin add org.apache.cordova.device
module.controller('MyCtrl', function($scope, $cordovaDevice) {
var uuid = $cordovaDevice.getUUID();
});
Within v2 it works like this:
import { Device } from 'ionic-native';
console.log('Device UUID is: ' + Device.uuid);
Reference: http://ionicframework.com/docs/v2/native/device/
Install:
#ionic-native/core
#ionic-native/device
enter link description here
ionic cordova plugin add cordova-plugin-device
npm install --save #ionic-native/device
Add this plugin to your app's module
// app.module.ts
import { Device } from '#ionic-native/device';
...
#NgModule({
...
providers: [
...
Device
...
]
...
})
export class AppModule { }
Usage
import { Device } from '#ionic-native/device';
constructor(private device: Device) { }
...
console.log('Device Model is: ' + this.device.model+
'\n Device UUID is: ' + this.device.uuid+
'\n Device serial is: ' + this.device.serial+
'\n Device platform is: ' + this.device.platform+
'\n Device version is: ' + this.device.version+
'\n Device manufacturer is: ' + this.device.manufacturer);
If won't run change "import { Device } from '#ionic-native/device';" for "import { Device } from '#ionic-native/device/ngx';"
And "this.device.uuid" for "Investigate"
Use these commands for run in browser
ionic build
ionic cordova platform add browser
cordova run browser
And works ! in these versions
in Browser
in Real Device
You could just use ionic.Platform.device() in your platform.ready function.
$ionicPlatform.ready(function {
console.log(ionic.Platform.device());// returns an object containing device uuid,version, platform, manufacturer ...
});
hope this helps someone :).
Regards.
http://forum.ionicframework.com/t/ionic-cordova-device-uuid/13652
You may only access cordova plugins within the ionic.Platform.ready() callback function:
angular.module('starter.controllers', [])
.controller('DashCtrl', function ($scope, $state, $cordovaDevice) {
var init = function () {
console.log("initializing device");
try {
$scope.uuid = $cordovaDevice.getUUID();
}
catch (err) {
console.log("Error " + err.message);
alert("error " + err.$$failure.message);
}
};
ionic.Platform.ready(function(){
init();
});
})
This is because Cordova plugins take some more time to load then the web application. The ionic.Platform.ready() callback is triggered as soon Cordova has fully loaded or immediately if it is already loaded.
wow found out what wrong i was doing... through this question. http://forum.ionicframework.com/t/problem-to-use-ngcordova-device-is-not-defined/11979
when we test on other device which has other platform than cordova supports this happens.
this was a useful discovery for me.
Have been struggling with this for hours today, install the cordova device plugin with:
cordova plugin add cordova-plugin-device
make sure you also reference the plugin in your config.xml:
<plugin name="cordova-plugin-device" source="npm" spec="~1.1.1" />

Categories

Resources