What I do wrong when I try to make an app with PhoneGap Build?
I want to make an app which open external link
I make in CMD phonegap project
phonegap create my-app
Here my config.xml and GAP plugin
<gap:plugin name="org.apache.cordova.inappbrowser" />
index.html BODY
<div class="app">
<h1>PhoneGap</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
window.open = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes');
}
</script>
Then I make a ZIP and load to PhoneGap Build
And make an APK...
But my APP doesn't open link.
What I do wrong?
You have a onDeviceReady function in your app object in your index.js. Try adding window.open there like this:
onDeviceReady: function() {
app.receivedEvent('deviceready');
window.open = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes');
},
Maybe you didn't install the plugin by running this command in the cmd:
phonegap plugin add cordova-plugin-inappbrowser
EDIT:
When your device is plugged in you can go to the link below and look at the console for errors:
chrome://inspect
Related
I have followed this article:
to get network information of the device in ionic app for android.
It works fine on browser but when I install the compiled apk in android phone, it gives an error that says Reference error : Connection is not defined. at line where I use $cordovaNetwork.isOnline();
I have been banging my head around and have done my due research and tried uninstalling and installing it in the order suggested but no help.
Help me fix this issue. This problem is probably not an issue with code and may be need some clever fix to get it working.
This same issue is being discussed here but I have not really understood where is that given piece of code coming from.
index.html :
<!DOCTYPE html>
<html ng-app="starter" >
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
<title></title>
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<!-- <link href="lib/ionic/css/angular-datepicker.min.css" rel="stylesheet"> -->
<link href="css/style.css" rel="stylesheet">
<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->
<!-- angular date picker css-->
<link href="lib/datePicker/css/angular-pickadate.css" rel="stylesheet">
<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="lib/ionic/js/highcharts-ng.js"></script>
<script src="lib/ionic/js/jquery.min.js"></script>
<script src="lib/ionic/js/highcharts.js"></script>
<script src="lib/ionic/js/ngStorage.min.js"></script>
<script src="lib/ngCordova/dist/ng-cordova.min.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>
<!-- your app's js -->
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
</head>
<body ng-controller="LoginCtrl">
<ion-nav-bar class="mob-bar-balanced">
<!-- <ion-nav-back-button>
</ion-nav-back-button> -->
</ion-nav-bar>
<ion-nav-view></ion-nav-view>
</body>
</html>
app.js:
app.factory('ConnectivityMonitor', ['$rootScope', '$cordovaNetwork', function($rootScope, $cordovaNetwork){
return {
isOnline: function(){
if(ionic.Platform.isWebView()){
$rootScope.online = $cordovaNetwork.isOnline();
return $cordovaNetwork.isOnline();
} else {
$rootScope.online = navigator.onLine;
return navigator.onLine;
}
},
isOffline: function(){
if(ionic.Platform.isWebView()){
$rootScope.online = $cordovaNetwork.isOnline();
return !$cordovaNetwork.isOnline();
} else {
$rootScope.online = navigator.onLine;
return !navigator.onLine;
}
},
startWatching: function(){
if(ionic.Platform.isWebView()){
$rootScope.$on('$cordovaNetwork:online', function(event, networkState){
$rootScope.online =true;
console.log("went online");
});
$rootScope.$on('$cordovaNetwork:offline', function(event, networkState){
$rootScope.online =false;
console.log("went offline");
});
}
else {
window.addEventListener("online", function(e) {
$rootScope.online =true;
console.log("went online");
}, false);
window.addEventListener("offline", function(e) {
$rootScope.online =false;
console.log("went offline");
}, false);
}
}
}
}]);
.config(function($stateProvider,$urlRouterProvider){
$stateProvider
.state('Login',{
url:'/login',
onEnter:["$state","$localStorage", '$rootScope' , '$ionicViewSwitcher',function($state,$localStorage, $rootScope, $ionicViewSwitcher){
if((typeof($localStorage.userInfo)!== 'undefined') && (Object.keys($localStorage.userInfo).length !== 0)) {
$ionicViewSwitcher.nextTransition('none');
$state.go("Deployment");
}
}],
templateUrl:'templates/login.html',
controller:'LoginCtrl',
resolve: {
online: function(ConnectivityMonitor){
return ConnectivityMonitor.isOnline();
}
}
})
I don't know about the tutorial you're mentioning, however, you may want to check the post I wrote which goes literally step by step telling you where and why you should put some piece of code (may be useful if you're just starting with Ionic): http://www.nikola-breznjak.com/blog/codeproject/check-network-information-change-with-ionic-famework/.
Also, I made the example code available freely on Github: https://github.com/Hitman666/IonicNetworkInfo. You can download the project (if you don't want to go through the steps yourself) build it for your device and test it on the device.
Here are the steps from the blog posts:
Start a new Ionic project by doing:
ionic start IonicNetworkInfo blank
Then, change the directory to the newly created IonicNetworkInfo:
cd IonicNetworkInfo
Install ngCordova with Bower:
bower install ngCordova
If by some chance you don’t have bower installed, you can install it with npm:
npm install bower -g
Open up the www/index.html file in your favorite editor, and add the reference to ngCordova (just above the cordova.js script):
<!-- This is what you should add, the cordova below you'll already have -->
<script src="lib/ngCordova/dist/ng-cordova.min.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>
Install the ngCordova network plugin by executing the following command in your Terminal/Command prompt (you should do this from the root directory of your app; so, in our case the IonicNetworkInfo directory):
cordova plugin add org.apache.cordova.network-information
To check if you have successfully installed the plugin, you can run the following command (from the root directory – I won’t be repeating this anymore; when I say you should run some command from the Terminal/Command prompt that, in this case, means from the root directory of the application):
cordova plugin list
You should see the following output:
> cordova plugin list
com.ionic.keyboard 1.0.4 "Keyboard"
org.apache.cordova.network-information 0.2.15 "Network Information"
Open up the www/js/app.js file and add ngCordova to the dependencies list, so that basically the first line looks like this:
angular.module('starter', ['ionic', 'ngCordova'])
Create a new controller in the www/js/app.js file called MyCtrl, with the following content:
.controller('MyCtrl', function($scope, $cordovaNetwork, $rootScope) {
document.addEventListener("deviceready", function () {
$scope.network = $cordovaNetwork.getNetwork();
$scope.isOnline = $cordovaNetwork.isOnline();
$scope.$apply();
// listen for Online event
$rootScope.$on('$cordovaNetwork:online', function(event, networkState){
$scope.isOnline = true;
$scope.network = $cordovaNetwork.getNetwork();
$scope.$apply();
})
// listen for Offline event
$rootScope.$on('$cordovaNetwork:offline', function(event, networkState){
console.log("got offline");
$scope.isOnline = false;
$scope.network = $cordovaNetwork.getNetwork();
$scope.$apply();
})
}, false);
})
In this controller you attach an event listener on the deviceready event (because it could be that the device would not have been yet initialized when this code runs) and you get the network information with:
$cordovaNetwork.getNetwork();
The information, about weather you’re connected to the internet is obtained with the following line:
$scope.isOnline = $cordovaNetwork.isOnline();
Then, you register two events $cordovaNetwork:online and $cordovaNetwork:online which trigger when the device gets online/offline. In them you then just update the $scope variables ().
Just for reference, the whole content of the www/js/app.js file should be:
// Ionic Starter App
// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
angular.module('starter', ['ionic', 'ngCordova'])
.run(function($ionicPlatform, $cordovaNetwork, $rootScope) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
})
.controller('MyCtrl', function($scope, $cordovaNetwork, $rootScope) {
document.addEventListener("deviceready", function () {
$scope.network = $cordovaNetwork.getNetwork();
$scope.isOnline = $cordovaNetwork.isOnline();
$scope.$apply();
// listen for Online event
$rootScope.$on('$cordovaNetwork:online', function(event, networkState){
$scope.isOnline = true;
$scope.network = $cordovaNetwork.getNetwork();
$scope.$apply();
})
// listen for Offline event
$rootScope.$on('$cordovaNetwork:offline', function(event, networkState){
console.log("got offline");
$scope.isOnline = false;
$scope.network = $cordovaNetwork.getNetwork();
$scope.$apply();
})
}, false);
});
In the index.html file, inside the ion-content tag paste the following content:
<div class="card">
<div class="item item-text-wrap">
<h1>Network: {{network}}</h1>
</div>
</div>
<div class="card">
<div class="item item-text-wrap">
<ion-toggle ng-model="isOnline" ng-checked="item.checked">
<h1 ng-show="isOnline">I'm online</h1>
<h1 ng-show="! isOnline">I'm offline</h1>
</ion-toggle>
</div>
</div>
Basically what we do here is we show the contents of the network variable (which is attached to the $scope via the controller). Also, by using the ion-toggle component we show the “I’m online” / “I’m offline” notifications.
Just for reference, the content of the whole index.html file should look like this:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->
<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="lib/ngCordova/dist/ng-cordova.min.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>
<!-- your app's js -->
<script src="js/app.js"></script>
</head>
<body ng-app="starter" ng-controller="MyCtrl">
<ion-pane>
<ion-header-bar class="bar-stable">
<h1 class="title">Ionic Blank Starter</h1>
</ion-header-bar>
<ion-content padding="true">
<div class="card">
<div class="item item-text-wrap">
<h1>Network: {{network}}</h1>
</div>
</div>
<div class="card">
<div class="item item-text-wrap">
<ion-toggle ng-model="isOnline" ng-checked="item.checked">
<h1 ng-show="isOnline">I'm online</h1>
<h1 ng-show="! isOnline">I'm offline</h1>
</ion-toggle>
</div>
</div>
</ion-content>
</ion-pane>
</body>
</html>
In order to test this application you should run it on your device (because you can’t disable network in iOS simulator). If you have an Android device plugged to your computer (and all the SDKs in place) you can run the following to commands to get your application running on your Android device:
ionic build android && ionic run android
Maybe you have not installed Network-Plugin. Please check this.
Now, I think it would be a better approach to use
$ionicPlatform.ready(function() {
$scope.network = $cordovaNetwork.getNetwork();
$scope.isOnline = $cordovaNetwork.isOnline();
$scope.$apply();
});
instead of document.addEventListener("deviceready", function () {
i'm new with PhoneGap build and i'm tryng to write a simple app with a button for facebook login. In PhoneGap build documentation i found is very simple to add this plugin
index.html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css" />
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha256.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-base64-min.js"></script>
<script src="js/facebookConnectPlugin.js"></script>
<script src="js/jq.js"></script>
</head>
<body>
<div data-role="page" id="contenuto">
<a href="index.html" data-role="button" class='bt_loginProprietario'>Login</a>
<a href="index.html" data-role="button" class='bt_loginFacebook'>Login con facebook</a>
</div>
</body>
</html>
jq.js
function loginFacebook(){
facebookConnectPlugin.login( ["email"],
function (response) { alert(JSON.stringify(response)) },
function (response) { alert(JSON.stringify(response)) };
};
$(document).ready(function(){
$('#contenuto').on('click','.bt_loginFacebook',function(){
loginFacebook();
});
});
in PG build i see the plugin correctly loaded but when i try to execute the app on my android phone nothing happens when i click on button. i tried to execute in my browser and i receive the error "cordova is not defined
thx for the help this is my first post, i don't understand what is wrong
update
with your suggestion my jq.js now has this code:
document.addEventListener("deviceready", function(){
$('#contenuto').on('click','.bt_loginFacebook',function(){
loginFacebook();
});
});
but when i press the button nothing happens, i saw an error in my chrome console that says cordova is not defined
help pls
This is very common for person new to cordova/phonegap.
You are using a facebook plugin for phonegap. Phonegap REQUIRES that you wait until the deviceready event fires before you access any phonegap or 3rd-party plugin services. This blog post explains in more detail.
FYI – Cordova events must be run after deviceReady
http://www.raymondcamden.com/2015/07/15/fyi-cordova-events-must-be-run-after-deviceready
I don't know why the plugin manager throw exec() call to unknown plugin: StatusBarNotification, I downloaded a example and tried to make it equal, but I couldn't.
I am using phonegap 3.5
config.xml
<plugin name="StatusBarNotification" value="com.phonegap.plugins.statusBarNotification.StatusBarNotification"/>
index.html
<body>
<div class="app">
<h1>PhoneGap</h1>
<button onclick="sendNotification()"> Mensaje</button>
</div>
</div>
</div>
<script type="text/javascript" src="js/statusbarnotification.js"></script>
<script type="text/javascript" charset="utf-8">
function sendNotification() {
window.plugins.statusBarNotification.notify("Sample Notification", "mensajito");
}
</script>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
It looks like the plugin you are using is not for Cordova/PhoneGap >= version 3.0.
Since 3.0, everything should be done via the CLI, including installing plugins: https://cordova.apache.org/docs/en/3.0.0/guide_cli_index.md.html
You then don't include references to any plugin javascript, as Cordova takes care of this.
You can search for compatible plugins on either of these 2 sites:
http://plugreg.com
http://plugins.cordova.io
Hopefully this helps.
So i've gotten jQuery 1.8.2 working with Phonegap no problem, but as soon as I add in jquery.mobile.1.2.0, the default Phonegap example breaks. the deviceready event stops firing.
index.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
<title>Hello World</title>
</head>
<body>
<div class="app">
<h1>Apache Cordova</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
</div>
<input type="text" name="firstname" id="firstname" />
Say Hello
<script type="text/javascript" src="cordova-2.4.0.js"></script>
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="js/jquery.mobile-1.2.0.min.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
function displayHello(){
var name = document.getElementById("firstname").value;
navigator.notification.alert("My name is "+ name);
}
</script>
</body>
</html>
index.js
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicity call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent('deviceready');
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}
};
So this is the default code example Phonegap comes with, and i've only added
<script type="text/javascript" src="js/jquery.mobile-1.2.0.min.js"></script>
in index.html.
Not sure what is going on as it seems other people have gotten Phonegap and jQuery Mobile to work just fine together.
I've tried dumbing the js down and just calling the deviceready event.
And I've tried following this solution and the other posted below it to no avail.
Correct way of using JQuery-Mobile/Phonegap together?
But the same thing happens. With jQuery Mobile deviceready never fires, without it, it fires fine.
Any help would be appreciated! Maybe i'm just missing something simple here.
I have also tried different version combinations of jQuery and jQuery Mobile with no luck.
I was running the Android Phonegap version and cordova-2.3.0. I recently tried upgrading to cordova-2.4.0 to see if that would help but nothing...
Update: No errors are thrown in the LogCat/DDMS
I had the same issue a while ago and I ended up throwing out the launcher of phonegap.
I suggest doing it like so :
<script type="text/javascript" src="cordova-2.4.0.js"></script>
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="js/jquery.mobile-1.2.0.min.js"></script>
<script type="text/javascript">
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
// your functions here
}
</script>
I hope it helps
Try putting scripts above jquery mobile library.:
<script type="text/javascript" src="cordova-2.4.0.js"></script>
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
function displayHello(){
var name = document.getElementById("firstname").value;
navigator.notification.alert("My name is "+ name);
}
</script>
<script type="text/javascript" src="js/jquery.mobile-1.2.0.min.js"></script>
updates:
<script type="text/javascript">
$(document).on('pageinit', function($){
app.initialize();
});
</script>
I noticed that the browser does not work, but using the emulator or PhoneGap build ondeviciready event and the same example works!
I had the exact same problem even after adding the libraries to the head, but as soon as I moved cordova.js below jquery-mobile, deviceready started firing again.
<head>
<script type="text/javascript" src="jqueryMobile/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="jqueryMobile/jquery.mobile-1.4.3.min.js"></script>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
</script>
</head>
I'm new to android programming and I'm trying to use Phonegap and jQuery Mobile.
I have configured Eclipse, Android SDK and Phonegap and this is working correctly but I have trouble with jQuery.
Here is my test page for jQuery:
<!DOCTYPE html>
<html>
<head>
<title>TEST</title>
<script type="text/javascript" charset="utf-8" src="cordova-2.2.0.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery.mobile-1.2.0.js"></script>
<script type="text/javascript" charset="utf-8">
var deviceReadyDeferred = $.Deferred();
var jqmReadyDeferred = $.Deferred();
document.addEventListener("deviceReady", deviceReady, false);
function deviceReady() {
$('#title2').html('THIS WORK');
deviceReadyDeferred.resolve();
}
$(document).one("mobileinit", function () {
jqmReadyDeferred.resolve();
});
$.when(deviceReadyDeferred, jqmReadyDeferred).then(doWhenBothFrameworksLoaded);
function doWhenBothFrameworksLoaded() {
$('#title2').html('THIS WORK');
}
</script>
</head>
<body>
<table width=100% height="50" border ="0">
<tr>
<td>
<div id="title">TEST</div>
<div id="title2">TEST2</div>
</td>
</tr>
</table>
</body>
</html>
I expected to get "TEST2" replace by "THIS WORK" but this is not the case when I launch the Android emulator.
May someone help me to find out what I'm doing wrong?
Thanks a lot for your help
Best Regards.
Florent
I confirm that I missed to include jQuery library.
Adding it solved my issue:
<script type="text/javascript" charset="utf-8" src="jquery-1.8.3.js"></script>