ChildBrowser onLocationChange not working - ios

Basically I am using ChildBrowser on iOS, it works almost perfectly. The problem is the onLocationChange, onClose and onOpenExternal don't work and for the life of me I can't figure out why.
Any help would be much appreciated
<script type="text/javascript" charset="utf-8">
var cb;
function onBodyLoad()
{
document.addEventListener("deviceready",onDeviceReady,false);
}
/* PhoneGap has been initialized and is ready to roll */
function onDeviceReady()
{
console.log("Device Ready");
cb = ChildBrowser.install();
}
function openChildBrowser(url)
{
console.log("New Url"+url);
try {
cb.showWebPage(url);
cb.onLocationChange = function(loc){ root.locChanged(loc); };
cb.onClose = function(){root.onCloseBrowser()};
cb.onOpenExternal = function(){root.onOpenExternal();};
}
catch (err)
{
alert(err);
}
}
function onCloseBrowser()
{
console.log("closed");
alert("In index.html child browser closed");
}
function locChanged(loc)
{
console.log("new loc:"+loc);
childBrowser.close();
alert("In index.html new loc = " + loc);
}
function onOpenExternal()
{
console.log("new loc:");
alert("In index.html onOpenExternal");
}
</script>
I have tried window.plugins.childBrowser.onLocationChange ect.

take out the root.onlocationChange, no need to have that.

Encapsulate the locChanged, onCloseBrowser, onOpenExternal into a main function to make correct calls because childbrowser when it´s opened doesn't have the scope of locChanged
for example:
var facebook = function() {
locChanged = funciton() {
//your code here
}
return {
"init": init,
"locChanged": locChanged,
"onCloseBrowser": onCloseBrowser,
"onOpenExternal": onOpenExternal
}
}();

Related

cordova-plugin-apple-watch plugin issue

Hi I am calling the init method of cordova-plugin-apple-watch plugin in our cordova application , but it is not returning to neither successhandler or errorhandler.
isWatchPairingExists: function() {
var deferred = new Deferred();
console.log("gdfgd");
applewatch.init(successHandler, errorHandler, "group.com.company.app");
function successHandler() {
console.log("rrrr");
alert("binding exist");
return deferred.resolve(true);
}
function errorHandler() {
console.log("ewrwrwr");
alert("binding does not exist");
return deferred.resolve(false);
}
return deferred.promise;
}
};
I am really grateful , if anyone can help on this.

YouTube iframe API - onReady and onStateChanged events not firing

I'm really having a frustrating time handling YouTube's iFrame API. Everything was working fine until yesterday, when I noticed my .playVideo() and .pauseVideo() functions throw an "undefined is not a function" error. Now, I can see that none of my functions appear to work... the events "onready" and "onstatechange" don't appear to be firing either. Here's my code:
function addVideo(index, url){
$.getScript('https://www.youtube.com/iframe_api', function(){
processPlayer();
});
function processPlayer(){
var videos = document.getElementById("videos");
var individ = document.createElement("div");
individ.setAttribute("class", "individ");
var vid = document.createElement("div");
vid.setAttribute("id","vid"+index);
individ.appendChild(vid);
videos.appendChild(individ);
var player;
function onYouTubeIframeAPIReady() {
console.log("Are we here at least?");
player = new YT.Player('vid'+index, {
height: '165',
width: '100%',
videoId: url,
playerVars: { 'controls': 0, 'showinfo': 0, 'rel': 0},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
window.players.push(player);
//individ.innerHTML+="Added by "+namesList[index];
individ.innerHTML+="<div style='float: left;'><span class='sname'>Let it Burn</span><br/><span class='aname'>Dave Matthews Band</span></div><div style='position: relative;'><img class='s_user' src='http://i.imgur.com/1AmnCp4.png'/></div>";
window.players.push(player);
}
onYouTubeIframeAPIReady();
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
//event.target.playVideo();
console.log("We're ready");
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
var done = false;
function onPlayerStateChange(event) {
console.log("HI?");
if(event.data === 0) {
if(window.currentIndex < window.players.length-1){
var videoID = window.players[window.currentIndex].getVideoUrl().split("v=")[1];
window.players[window.currentIndex].cueVideoById(videoID);
window.currentIndex++;
window.players[window.currentIndex].playVideo();
}
} else if(event.data === 2 ){
onYouTubeIframeAPIReady();
}
if(!window.playing){
//alert('playing');
window.playing = true;
} else {
//alert('stopping');
window.playing = false;
}
}
function stopVideo() {
player.stopVideo();
}
}
}
Any ideas why? I'd really appreciate some help on this. The video itself loads fine, and the YTPlayer object can be called from console... yet these functions don't work, and onready/onstatechange don't fire. The iframes by default have the "origin=" bit in there, so that fix didn't work either.
I see several problems in your code, but I'm not sure which one of them is the one that's bothering you.
First of all, you're not supposed to call onYouTubeIframeAPIReady directly.
Instead, you should execute the following and let the browser do it asynchronously:
var scriptElement = document.createElement("script");
scriptElement.src = "http://www.youtube.com/iframe_api";
var firstScriptElement = document.getElementsByTagName("script")[0];
firstScriptElement.parentNode.insertBefore(scriptElement,firstScriptElement);
Second, I believe that you should initialize at least the following player parameters:
playerVars:
{
"enablejsapi":1,
"origin":document.domain,
"rel":0
},
events:
{
"onReady":onPlayerReady,
"onError":onPlayerError,
"onStateChange":onPlayerStateChange
}
Here is the complete relevant piece of code that I have been using:
<body onload="LoadYouTubeIframeAPI()">
<div id="player">Loading Video Player...</div>
<script type="text/javascript">
var player = null;
function LoadYouTubeIframeAPI()
{
var scriptElement = document.createElement("script");
scriptElement.src = "http://www.youtube.com/iframe_api";
var firstScriptElement = document.getElementsByTagName("script")[0];
firstScriptElement.parentNode.insertBefore(scriptElement,firstScriptElement);
}
function onYouTubeIframeAPIReady()
{
var playerParams =
{
playerVars:
{
"enablejsapi":1,
"origin":document.domain,
"rel":0
},
events:
{
"onReady":onPlayerReady,
"onError":onPlayerError,
"onStateChange":onPlayerStateChange
}
};
player = new YT.Player("player",playerParams);
}
function onPlayerReady(event)
{
...
}
function onPlayerError(event)
{
...
}
function onPlayerStateChange(event)
{
...
}
</script>
</body>

Phonegap show html if not connected

Here is my code at the minute:
Using phonegap 2.9
<head>
</head>
<body>
<script charset="utf-8" src="js/cordova.js"></script>
<script>
document.addEventListener("deviceready", onDeviceReady, false);
document.addEventListener("online", onOnline, false);
document.addEventListener("offline", onOffline, false);
// PhoneGap is loaded and it is now safe to make calls PhoneGap methods
//
function onDeviceReady() {
console.log("Device Ready");
}
// Handle the online event
//
function onOnline() {
document.location = 'http://app.dadad.com';
}
function onOffline() {
console.log("Offline");
}
</script>
</body>
However right now I just get a white screen whether i'm connected or not. Eventually what I would like is to display some html when the user is not connected.
So in conclusion:
Would like to fix the function as it is not working
Would like to show html when not connected.
The Online/Offline events do not fire onload. They are there for when you are in app (complete load) and then lose or gain connection. I have gotten around this by doing an initial connection check on load, like this:
function checkConnetcion() {
var networkState = navigator.connection.type;
var states = {};
states[Connection.UNKNOWN] = false;
states[Connection.ETHERNET] = true;
states[Connection.WIFI] = true;
states[Connection.CELL_2G] = true;
states[Connection.CELL_3G] = true;
states[Connection.CELL_4G] = true;
states[Connection.CELL] = true;
states[Connection.NONE] = false;
var connectionStatus = states[networkState];
if(connectionStatus) {
//Do something if connected
}
else{
//Do something if not connected
}
}
Then add this your onready function:
// PhoneGap is loaded and it is now safe to make calls PhoneGap methods
//
function onDeviceReady() {
console.log("Device Ready");
checkConnetcion();
}
You can make use of Jquery ajax and send a dummy request before sending actual request. If you get and Error Code as '0' it means there is no internet connectivity.
$.ajax({
url: 'TestUrl',
type: 'GET',
success: function (data) {
// Go ahead with you request
},
error: function (x, y, z) {
if (x.status == 0) {
alert("Please connect to the internet");
}
else{
alert("Other Error Occured")
}
}
});
Secondly you can also make you of HTML 5 navigator
var condition = navigator.onLine ? "ONLINE" : "OFFLINE";
But it will show ONLINE when WIFI doesn't provide Internet connection.
Cordova connection object will also show WIFI if there is no internet connection

Phonegap:Childbrowser not working

I am working with ios phonegap application. I created a new project with phonegap2.4.0.
It was success.But my issue is childbrowser is not coming and shows following error in console:
OPENING URL:INVALID
I have done all the steps needed to include a childbrowser in the project and it is working fine in an old phonegap project.
How can i fix the problem??
function onBodyLoad() {
document.addEventListener("deviceready", onDeviceReady,false);
}
function onDeviceReady() {
cb = ChildBrowser.install();
}
function helo() {
alert("hiiii");
cordova.exec(null,null,"ChildBrowser","showWebPage",['google.com']);
}
function onDeviceReady() {
if(window.plugins.childBrowser == null)
{
ChildBrowser.install();
}
}
Or You can use InAppBrowser instead of childbrwoser.
For close browser use ref.close();
var ref = window.open("google.com", 'random_string', 'location=no');
ref.addEventListener('loadstart', function(event) {
console.log(event.type + ' - ' + event.url);
} );
ref.addEventListener('loadstop', function(event) {
console.log(event.type + ' - ' + event.url);
} );
ref.addEventListener('exit', function(event) {
//alert(event.type);
} );
function onBodyLoad()
{
document.addEventListener("deviceready", onDeviceReady,false);
}
function onDeviceReady()
{
cb = ChildBrowser.install();
}
function helo()
{
alert("hiiii");
cordova.exec(null,null,"ChildBrowser","showWebPage",['http://google.com']);
}

How to return a FileContent with FileReader from function in PhoneGap iOS?

If, for example, i will that fileReader must return a content value of file, i get back only empty string. Global variable for me impossible to use, only local. Is it possible?
What do i wrong?
Edit:
function onDeviceReady() {
console.log("==> DEVICE READY");
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFSSuccess, fileErrorMSG);
}
function onFSSuccess(fs) {
fileSystem = fs;
}
function readlocalFile(fileName) {
var core = "";
fileSystem.root.getFile(fileName, {create: false}, function(f) {
f.file(function(e) {
var reader = new FileReader();
reader.onloadend = function(evt) {
var res = $.parseJSON(evt.target.result);
core = res;
};
reader.readAsText(e);
});//f.file()
}, fileErrorMSG);
return core;
}
function loadDefaultCore(url) {
if (url) {
var myCore = readlocalFile(url);
console.log(myCore); // **output - empty string!!!!!!!!**
} else {
alert('can not load default core');
}
}
Thanks!
Well there a bunch of things to consider. In brief:
First, to read a file you need to get the file system.
Second, on success you need to get the file entry using the file system.
Third, on success read the file using the file entry.
The three have to be chained via the functions callbacks.
You are getting empty as you file is not being read.
I follow the chain an after the onFSSuccess function the chain is broken, the readLocalFile function is not being called, just add it after you assign your file system to the fileSystem variable which I assume is a global variable. Or call your function loadDefaultCore, I am not sure which one you really want to call first.
It will help you if you add more console log messages in each function so you can actually debug the problem easily.
Also, did you have your document even listener attached to the device ready function?
Any messages, errors warnings in the console?
From the phonegap file api, follow this and you will be safe. Check the doc for the phonegap version you are working on.
http://docs.phonegap.com/en/2.4.0/cordova_file_file.md.html#FileReader
<script type="text/javascript" charset="utf-8">
// Wait for Cordova to load //function onLoad() { document.addEventListener("deviceready", onDeviceReady, false); }
// Cordova is ready //function onDeviceReady() { window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); }
function gotFS(fileSystem) { fileSystem.root.getFile("readme.txt", null, gotFileEntry, fail); }
function gotFileEntry(fileEntry) { fileEntry.file(gotFile, fail); }
function gotFile(file){ readDataUrl(file); readAsText(file); }
function readDataUrl(file) { var reader = new FileReader(); reader.onloadend = function(evt) { console.log("Read as data URL"); console.log(evt.target.result); }; reader.readAsDataURL(file); }
function readAsText(file) { var reader = new FileReader(); reader.onloadend = function(evt) { console.log("Read as text"); console.log(evt.target.result); }; reader.readAsText(file); }
function fail(evt) { console.log(evt.target.error.code); }
</script>

Resources