How do I use PhoneGap with Worklight? - javascript

I wanted to know how Worklight uses PhoneGap as it's something I am required to do shortly. I hear that PhoneGap is 'build into' Worklight. If this is so, do I have to declare I'm using it, as in the below?:
<script type="text/javascript" charset="utf-8" src="cordova-1.9.0.js"></script>
And for that matter, if I do have to declare it, how do I know what version of PhoneGap it is utilising? If I don't have to declare it, will it just recognise my PhoneGap calls?
Any advice on this would be super as I just cannot find the answers on either the Worklight or PhoneGap websites.
Thanks!

Latest Worklight have a Cordova (formerly known as PhoneGap) 1.6.1
It's embedded automatically once you build your iOS/Android application, so you really do not need to add it's JS manually.
All of your Cordova calls will be recognized.

#creights there is not need to add Cordova.js internal into your Worklight application. By default Cordova plugin is included in your worklight app. You need to just use the functionality of plugin like camera,accelerator etc for an example i have a piece of code how to include camera in ur worklight application.
function takePicture() {
navigator.camera.getPicture(
function(data) {
var img = dom.byId('camera_image');
img.style.visibility = "visible";
img.style.display = "block";
//img.src = "data:image/jpeg;base64," + data;
img.src = data;
dom.byId('camera_status').innerHTML = "Success";
},
function(e) {
console.log("Error getting picture: " + e);
dom.byId('camera_status').innerHTML = e;
dom.byId('camera_image').style.display = "none";
},
{ quality: 50, destinationType: navigator.camera.DestinationType.FILE_URI, sourceType : navigator.camera.PictureSourceType.CAMERA});
};
Just copy the code into AppMain.js in ur js folder.

Just to update, The newest version on IBM Worklight is PhoneGap 2.2. It just happened this month (Dec 2012). When you write your code, it will auto fill (on default prefs).
For example
typing navigator.noti....will popup with the notification options for the alert and confirmation calls and its parameters (with some comments).

Related

IONIC - How to open Application's feedback page in Play Store from Application using JavaScript

In my Ionic Application, I need to open my other application link in Play store.
I have tried following so far :
window.open('market://details?id=com.myapp.something', '_self')
And
window.open('market://details?id=com.myapp.something', '_system', 'location=no');
Above links opens in InnAppBrowser, I need them to open in playstore itself.
Any suggestions?
I found that you can open it in your system Browser with Package ID and it will automatically redirect you to respective application store.
$window.open("https://play.google.com/store/apps/details?id=your-app-package-name&hl=en","_system");
This worked for me the best.
EDIT :
There is a plugin available which might help : Launch Review
Redirect IONIC application to play store
window.open("https://play.google.com/store/apps/details?id=com.carClient.bookMyDreamCar","_system");
open playstore in your application
window.location.assign('https://play.google.com/store/apps/details?id=com.carClient.bookMyDreamCar')
If you want to open market apps for rating reviews then will be better to use this plugin instead
Ionic V3: https://ionicframework.com/docs/v3/native/launch-review/
Ionic >= V4: https://ionicframework.com/docs/native/launch-review
It has in app review for ios >10.3 (higher changes to get a review) and simply opens google play market for android.
Dependency injection:
import { LaunchReview } from '#ionic-native/launch-review';
constructor(
private _platform: Platform,
private _launchReview: LaunchReview
) { }
Implementation:
appId = null;
if (this._platform.is('android')) {
appID = '_COM.ANDROID.PACKAGE.NAME_';
} else if (this._platform.is('ios')) {
appID = '_APPLEID_';
}
if (appID) {
if (this._launchReview.isRatingSupported()) {
// For iOS > 10.3
this._launchReview.rating().then((result) => {
alert(result);
});
} else {
this._launchReview.launch(appID);
}
}
2020 Answer. Ionic 4.
Only window.location.assign helped for me. Working both iOS and Android. iOS URL should be itms-apps://itunes.apple.com/app/${iosITunesAppId}, Android one https://play.google.com/store/apps/details?id=${packageName}. packageName can be obtained using cordova-plugin-app-version plugin.
UPD: looks as I found why methods like window.open('market://details?id=com.myapp.something', '_system'); didn't work for me. Looks as they require cordova-plugin-inappbrowser plugin to be installed. I haven't that plugin installed in my app so that method didn't work.

Call Swift function from Cordova

I have a native iOS app written in Swift and it works on standalone. However, currently, I am exploring Cordova and developing a plugin for it using the Swift codes that I already have. Let's say my native Swift code is the following and the function that I want to call is startDataLog():
func startDataLog() {
// Set file logging parameters and do some logging etc...
let fileName = "log_\(dateFormatter.string(from: Date())).log"
let filePath = URL(fileURLWithPath:
(NSSearchPathForDirectoriesInDomains(.documentDirectory,
.userDomainMask, true)[0] as
NSString).appendingPathComponent(fileName))
//do other stuffs here...
}
Assuming that the plugin.xml and the bridging header are correctly configured for the new plugin:
First, I need to add this in www/plugin.js that calls the iOS codes. In my case startDataLog:
var exec = require('cordova/exec');
var serviceName = 'DriverAppPlugin'
module.exports = {
'startDataLog': function (successCallback, errorCallback) {
cordova.exec(successCallback, errorCallback, serviceName,
'startDataLog', [])
},
}
Then in my native Swift code I will have to add the following:
#objc(CentralPlugin) class CentralPlugin: CDVPlugin {
func startDataLog(command: CDVInvokedUrlCommand) {
// Set file logging parameters and does some extras
let fileName = "log_\(dateFormatter.string(from: Date())).log"
let filePath = URL(fileURLWithPath:
(NSSearchPathForDirectoriesInDomains(.documentDirectory,
.userDomainMask, true)[0] as
NSString).appendingPathComponent(fileName))
//do other stuffs here...
}
}
What is unclear to me is if this is what needs to be done to the native Swift code or the function that I want to call? Is adding
(command: CDVInvokedUrlCommand)
to the func is correct or am I doing something terribly wrong here. I am new to Cordova and as a matter of fact, there are not many tutorials that are based on integrating Cordova + Swift.
Any feedback or directions will be really helpful to kickstart my plugin development. Please suggest.

Can I use gamepads with Node-Webkit (NW.js)?

I am building a NW.js (Node-Webkit) dashboard app which I want to be able to control with a game controller (for example: XBox 360 controller or Logitech controller).
I'm calling the following onready but when I debug its not recognizing any gamepads.
angular.element(document).ready(function() {
if(canGame()) {
var prompt = "To begin using your gamepad, connect it and press any button!";
$("#gamepadPrompt").text(prompt);
$(window).on("gamepadconnected", function() {
$("#gamepadPrompt").html("Gamepad connected!");
console.log("connection event");
});
$(window).on("gamepaddisconnected", function() {
console.log("disconnection event");
$("#gamepadPrompt").text(prompt);
});
}
});
function canGame() {
return "getGamepads" in navigator;
}
When I debug the code, it doesn't appear to be detecting any gamepads. I also try:
navigator.webkitGetGamepads()
but it doesn't show any gamepads being detected either.
Has anyone successfully used gamepads with a NW.js app?
I would greatly appreciate some help getting this to work.
I found the answer to my issue. Gamepads are indeed compatible with NW.js. I however, was using an old version (~v0.08).
The code I posted earlier when running with NW.js v0.12.2.

Cordova ibeacon; Send local notification after the app got killed, but does not work on android

I am using Cordova / Phonegap iBeacon plugin with ionicframework at my cordova project. I am tryin to send a local notification both on android and ios with cordova local notification plugin while entering monitored region , when the app is killed.
Here is my code :
document.addEventListener("deviceready", onDeviceReady, false);
function didDetermineStateForRegion(pluginResult) {
}
function didStartMonitoringForRegion (pluginResult) {
}
function didExitRegion(pluginResult) {
$cordovaLocalNotification.add({
id: 30244234234,
title: "Good By!",
text: "Hope to see you again."
}).then(function () {
});
}
function didEnterRegion (pluginResult) {
$cordovaLocalNotification.add({
title: "Welcome",
text: "Tap to launch app"
}).then(function () {
});
};
function didRangeBeaconsInRegion (pluginResult) {
}
function onDeviceReady() {
// Now safe to use device APIs
function createBeacon(uuid,nofiyState) {
var uuid = uuid; // mandatory
var identifier = 'estimote'; // mandatory
// throws an error if the parameters are not valid
var beaconRegion = new cordova.plugins.locationManager.BeaconRegion(identifier, uuid);
beaconRegion.notifyEntryStateOnDisplay = true;
return beaconRegion;
}
var delegate = new cordova.plugins.locationManager.Delegate();
delegate.didDetermineStateForRegion = didDetermineStateForRegion;
delegate.didStartMonitoringForRegion = didStartMonitoringForRegion;
delegate.didRangeBeaconsInRegion = didRangeBeaconsInRegion;
delegate.didEnterRegion = didEnterRegion;
delegate.didExitRegion = didExitRegion;
var beaconRegion = createBeacon('02681445-8D1B-4F58-99D4-B25F4B129A58',true);
// var beaconRegionBlue = createBeacon('02681445-8D1B-4F58-99D4-B25F4B129A58',1,,true);
cordova.plugins.locationManager.setDelegate(delegate);
// required in iOS 8+
//cordova.plugins.locationManager.requestWhenInUseAuthorization();
cordova.plugins.locationManager.requestAlwaysAuthorization();
cordova.plugins.locationManager.startMonitoringForRegion(beaconRegion)
.fail(console.error)
.done();
}
cordova plugins :
com.unarin.cordova.beacon 3.3.0 "Proximity Beacon Plugin"
de.appplant.cordova.plugin.local-notification 0.8.1 "LocalNotification"
nl.x-services.plugins.socialsharing 4.3.16 "SocialSharing"
org.apache.cordova.console 0.2.13 "Console"
org.apache.cordova.device 0.3.0 "Device"
cordova version : 4.3.0
this works fine for ios even if the app is killed but on android notifications cames only if app in the background. When i kill the app from task manager on android i never seen any local notification.
Is it possible to receive notification on android even if app is killed ?
thanks for help.
lets clear some stuff , there are states that yo are confusing :
App as a service
App running in background (i.e minimized).
App killed (not running at all)
in all cases the 3rd state when you kill app ( via long press back button in custom roms , or force stop from app menu in your OS ) , the app is simply removed from memory , no code is being excuted !
what usually is done in this case is automatically relaunching the service after it has been stopped check this answer , and as you can read :
it is really very bad pattern to run service forcefully against the
user's willingness.
there are so many cordova plugins to create BroadcasteReceiver , however the simple answer to your question , if app is killed it is not possible to receive notification .
But you should consider this: if user kills your app , it means it was done intentionally , so you shouldnt really worry if your app will work or not , as this is the user's issue , and not yours as a developer.

How do I adapt the WinRT JScript RDP sample to XAML?

The WinRT JScript sample "Remote Desktop app container client sample" runs the RDP activeX component. This sample is similar to the RDP App downloaded from Windows Store. The sample is contained in MSDN Windows-8-Modern-Style-App-Samples. As you can see from the code the JScript is doing ActiveX in a WinRT app!!! Does any one know how to adapt this sample to XAML ?
createClientControl: function (desktopWidth, desktopHeight) {
// Initialize RDP client control.
// Create object element.
var clientControlObject = document.createElement("object");
// Register for ActiveX events.
clientControlObject.addEventListener("readystatechange", function (e) {
if (clientControlObject.readyState !== 4) {
WinJS.log && WinJS.log("Error: ActiveX control readyState is not 4. ReadyState: " + clientControlObject.readyState, "sample", "error");
}
}, false);
clientControlObject.addEventListener("error", function (e) {
WinJS.log && WinJS.log("Error in loading the ActiveX control", "sample", "error");
}, false);
// Set object's classid to RDP client control's s CLSID.
clientControlObject.classid = "CLSID:EAB16C5D-EED1-4E95-868B-0FBA1B42C092";
clientControlObject.id = "clientControl";
// Set object's height and width.
clientControlObject.width = desktopWidth;
clientControlObject.height = desktopHeight;
// Add the element to DOM.
var clientControlRegion = document.getElementById("clientControlRegion");
clientControlRegion.appendChild(clientControlObject);
return clientControlObject;
},
I think the easiest way is to download the C# sample and see how they did it (instead of translating the JS version) http://code.msdn.microsoft.com/windowsapps/Windows-8-Modern-Style-App-Samples/file/60705/34/Windows%208%20app%20samples.zip
The documentation says that Remote Desktop Services in this context is designed for Javascript:
Developer audience
Remote Desktop Services objects for Windows Store apps are designed for use by Windows
Store apps using JavaScript.
As far as I know there is no C# sample available, which was probably your question.
Because XAML has nothing to do with the implementation of RDP.

Categories