I am trying to get a compass function working on my app with no success. Judging by the errors I am inclined to think that the plugin is not being added or the device is not ready. But as far as I can tell I have done both things. I am using phonegap build, so the only code I use to include it is:
<gap:plugin name="org.apache.cordova.device-orientation" version="0.3.9" />
Which as far as I know is all that is required. When I click on the plugins tab in the phonegap build page for my app it shows up as being there.
watchDirection = null;
//Phonegap is ready
var whenDeviceReady = function(){
console.log("deviceready");
console.log(navigator.compass);
var findMyDirection = function(){
console.log("find my heading fired");
watchDirection = navigator.compass.watchHeading(
//onSuccess
function(heading){
console.log(heading);
var magnetDirection = heading.magneticHeading;
$("#movingCompass").css("transform", "rotate(" + magnetDirection + "deg)");
},
//onError
function(error){
console.log('code: ' + error.code +' message: ' + error.message);
},
//Update Frequency
{ frequency: 1000});
}
findMyDirection();
}
//Wait for phonegap to load
document.addEventListener("deviceready", whenDeviceReady, false);
The above code works as expected with the exception of the compass object. The console.log(navigator.compass); returns as undefined. And there is an error on the line which includes watchDirection = navigator.compass.watchHeading( saying Uncaught TypeError: Cannot read property 'watchHeading' of undefined.
It gets listed in the phonegap build list of installed plugins, and I already have the geolocation working with the same code format I have posted above.
#Marty.H, I have working demos here: http://codesnippets.altervista.org/examples/phonegap/demos/PUBLIC.Apps.html Go to the lower half of the page and try pre-built app, Phonegap-Compass-Test. If it works, the top half of the page has the links to github where you can get the source code. --Jesse
Related
I'm building a Cordova Ionic application which fetches JSON response on button click and display it. It's working fine in browser but it's not displaying anything in android.
angular.module('starter', ['ionic'])
.config(function($ionicConfigProvider) {
$ionicConfigProvider.navBar.alignTitle('center'); //align title in center
})
.controller('ControllerOne',[ '$scope', 'freshlyPressed', Ctrl])
.service('freshlyPressed', ['$http','$log', freshlyPressed]);
function Ctrl($scope, freshlyPressed){
$scope.refreshClicked = function(){
freshlyPressed.getBlogs($scope);
}
};
function freshlyPressed($http,$log){
this.getBlogs = function($scope){
$http.jsonp("https://public-api.wordpress.com/rest/v1.1/freshly-pressed?callback=JSON_CALLBACK")
.success(function(result, posts){
$scope.posts = result.posts;
});
};
};
How would I know if any exception occurred while testing the app in android?
[Edit] I'm new to Android & Cordova.
The only way you can find out if something goes wrong is if you add a .error function after the .success function, otherwise errors will not be caught.
Here is a code example for your current situation:
function freshlyPressed($http,$log){
this.getBlogs = function($scope){
$http.jsonp("https://public-api.wordpress.com/rest/v1.1/freshly-pressed?callback=JSON_CALLBACK")
.success(function(result, posts){
$scope.posts = result.posts;
})
.error(function(error){
console.log(error);
});
};
};
Although, I would highly recommend you switch to the .then() function, as .error and .success have been deprecated as described here.
You can debug your android apps with Chrome using DevTool.
To enable Remote Debugging follow these Remote Debugging Devices Documetation
.
To use it follow Chrome Inspector (Ionic Docs).
Hope this may help you.
I am trying to check disk space available in mobile using below code:
cordova.exec(function(result) {
var diskSizeInMB = result/1024;
alert(diskSizeInMB)
}, function(error) {
alert("Error: " + error);
}, "File", "getFreeDiskSpace", []);
In Android device it gives me correct result, whereas if I use iPhone/iPad it always returns me 0 as a output. Can anyone guide me how to resolve this issue? Or is there a way to check the disk size in iOS using cordova without writing custom plugin? To make this happen I haven't changed any configuration changes
It was a bug, but was fixed long time ago.
Old answer:
I confirm there is a bug on the code, the problem is getFreeDiskSpace is undocumented and cordova team want to remove the native code as getFreeDiskSpace isn't part of the w3c file API.
If you want to fix the bug for you app, go to CDVFile.m and change the line
NSNumber* pNumAvail = [self checkFreeDiskSpace:self.appDocsPath];
to
NSNumber* pNumAvail = [self checkFreeDiskSpace:self.rootDocsPath];
on the getFreeDiskSpace method
I am working on Cordova hybrid mobile app currently am doing in android app it runs fine now when i make same app for window phone it is not perform any functionality.
for make WP8 I follow this link after that i copy my all file of www folder to new generated www in Visual Studio project.
But when i ran app it just shows its first page only and not performing any functionality.
So what steps did i miss ?
On the click of button I call following function
$('#contactBackupBtn').on('click',function(){
$('#p2').append("Going to be backup");
sm_sync.backupAllTheContacts(function(){
$('#p4').append("After Contact Backup Function Finished ");
});
});
From above function it calls the following one
backupAllTheContacts:function(callback) {
$('#p3').append("IN backupAllTheContacts");
navigator.contacts.find(["*"], function(contacts) {
$('#p3').append("IN Contact Success");
callback();
}, sm_sync.onError, {"multiple": true});
}
onError:function(error) {
$('#p1').empty();
$('#p1').append(error.code);
$('#p1').append(error.message);
}
When i execute it, it shows this message IN backupAllTheContacts and ths Going to be backup but not showing any success or error messages. what should i do to make it run.
(This is a small part of my app it is runnng perfact in Android Emulator but not n windows
I need help i am stuck here)
use console.log is not support in windows phone so use this and use localStorage like this. try to run on deviceready
document.addEventListener("deviceready", function () {
/* This is for Console.log support in WP */
if (typeof window.console == "undefined") {
window.console = {
log: function (str) {
window.external.Notify(str);
}
};
}
var key0;
/* manage localstorage this way */
if (typeof (window.localStorage) !== "undefined") {
localStorage.setItem('lastname', "Smith");
localStorage.firstname = "One";
if (localStorage.getItem('objectKey1') != null || localStorage.getItem('objectKey1') != undefined) {
key0 = window.localStorage.getItem('objectKey1');
}
}
},false);
When i build my WP8 app i ran into somewhat an issue same as this. it's because of the jQuery i used. Then i updated my jQuery to the latest and its all started working fine.
Please check the jQuery documentation and the version you are using. Or you just put up your code in here so that we can have a closer look.
"But when i ran app it just shows its first page only and not performing any functionality.
So what steps did i miss ?"
Without the code how can we say what steps did you missed ???
After hours of search I finally decided to post a question here.
I am using cordova/phonegap version 3 library and trying to build an app based on geolocation.
What I need to do is to check if the user has their GPS enabled before they can do something else but this doesn't seems to be possible as far as i can see.
The code I am using is:
function onDeviceReady() {
// Throw an error if no update is received every 30 seconds
var options = {maximumAge: 0, timeout: 10000, enableHighAccuracy: true};
watchID = navigator.geolocation.watchPosition(onSuccess, onGeoError, options);
}
function onSuccess(position) {
//DO WHATEVER
}
function onGeoError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
I expect an error to be thrown with error.POSITION_UNAVAILABLE message but no matter what I do TIMEOUT error (error.TIMEOUT) gets thrown every single time. I even put the device in flight mode with everything (gps, etc) turned off and still got TIMEOUT error.
Can anyone please suggest a better way to achieve this.
The problem with my application was that I didn't run the build command (cordova prepare) after installing the plugin. Backup your www folder before running 'cordova build' command as this will replace www folder with default content
I AM creating a phonegap android app. I am creating this app using help of this tutorial. When i am istalling this app on device its onDeviceReady function is not working. It is only showing coneecting to device. My onDeviceReady function is below:-
onDeviceReady: function() {
app.receivedEvent('deviceready');
alert('device ready');
try {
var pushNotification = window.plugins.pushNotification;
pushNotification.register(app.successHandler, app.errorHandler,{"senderID":"41327727848","ecb":"app.onNotificationGCM"});
} catch (ex) {
alert('error: ' + ex);
}
},
It does not alert anything. I can't find the error.
Here are the suggestions you can try:
make sure you have added cordova.js into your html file.
deviceready function should be called after cordova.js is loaded.
the link you've provided is using old version version of plugin.(link-old plugin).I advice you to follow the new one which is this