Reputation: 632
I can open my window, and do a lot of things inside of it, for example login via Linkedin or Facebook. But I cannot close it to return to my app.
1/ I tried to catch events to track the url and close the window when the url contains a particular keyword. But the event is never fired. I never have any alert.
var ref = window.open(url, '_blank', 'location=no');
ref.addEventListener('loadstart', function(event)
{
alert(event.url);
});
ref.addEventListener('loadstop', function(event)
{
alert(event.url);
});
2/ So I tried to find a way to check ref.location.url from the first window every n seconds. But ref.location.url doesn't exist. I don't see a way to access the inapp current url from the parent window.
3/ I had the idea to give a name to the inapp window. But the inappbrowser plugin doesn't allow to give a name to the window to open. So the parent window cannot check the url of the child window that way as well.
4/ I tried to ask the child inapp window to close : cordova doesn't want a javascript to close the current window.
self.close(); => cannot
window.close(); => cannot
5/ I tried to revert to cordova.js 2.9.0-0-g83dc4bd , it didn't work as well.
So, I can open a pop up inside my app and use third parties Oauth but then I'm stuck inside this pop up and I have no way to return to my app.
I checked almost everything I could find in the internet and I absolutely don't see how to do now.
I use cordova.js 3.5 Android build , and I install inappbrowser with
cordova plugin add org.apache.cordova.inappbrowser
Everything is standard, cordova.js is correctly loaded, and I can see in the logs InappBrowser is correctly used when I call it.
08-08 16:20:20.594: D/InAppBrowser(8496): target = _blank
Errr... Help?
:)
Upvotes: 2
Views: 10222
Reputation: 632
I could find a way to solve this problem. It's weird and biased. But as I see I am not the only one, I'm writing here my solution and the way I could solve that bug.
First, I added another listener :
ref.addEventListener('exit', function(event)
{
if (debug) alert('exit');
});
Then, I used the inject script functionality and I called it every 5 seconds;
function getStateSecondWindow()
{
ref.executeScript(
{code: "localStorage.getItem('loginOauth')"},
function(data)
{
alert(data);
}
);
}
setInterval(getStateSecondWindow, 5000);
At that stage, I could see something weird in the logs : the first call to getStateSecondWindow was blocked. But then /everything/ started to work. I suddenly had all my event listeners working correctly. It was like I had a jam somewhere and the executeScript() just unblocked everything.
This solution worked for my 4 projects. The behaviour was the same for each of them.
In case the event had problems again, I used a double system to check my second page and find a way to close it at the right moment : in the second page, I used the local storage to set the value of my var : localStorage.setItem( "loginOauth", 'OK');
In my first page, I used the execute script to go to read that local storage :
function getStateSecondWindow() {
ref.executeScript(
{code: "localStorage.getItem('loginOauth')"},
function(data)
{
if (data=='OK')
{
//do what I need to do
ref.close();
}
}
);
With this system, I was not dependent of the (sometimes not) firing event. It works even if the page in the inappBrowser window comes from another server : as the javascript is interpreted in the second window, we are in the same domain where the original localStorage was made (no crossbrowsing problem).
So this is how I could go thought the inAppBroswer not firing events and find a way to close the second window when needed. Hope it helps :)
Upvotes: 3