1

I am writing some code in IBM's Worklight which uses Apache Cordova.

I am trying to open the InAppBrowser, and instead of having the user click "done" when finished, I want it to automatically close itself when a specific URL is loaded. Is this possible?

The scenario:
I basically direct the user to twitter.com (easier than using oauth process) and just have the user use twitter within the InAppBrowser browser, but once the user has logged in (the url becomes the news feed url) I want to redirect the user directly to the post URL, and once posted, back to the app, if possible.


Edit (May 1st, 2014)
Following the suggestion in the answer below, I am able to direct the user to the post page, however as soon as it finishes loading it redirects to the home page, without actually being able to send a tweet. Is there a way to fix this? Also, the InAppBrowser never exits.

Also, the below code only works if the user is already logged in. Is there a way to say like, if the user is not logged in go to the main page, if they are go to the post page?

// open InAppBrowser w/out the location bar
var ref = window.open('http://mobile.twitter.com/compose/tweet', '_blank', 'location=no');

// attach listener to loadstart
ref.addEventListener('loadstart', function(event) { 
    var urlSuccessPage = "http://mobile.twitter.com";
    if (event.url == urlSuccessPage) {
    ref.close();
    }
});
Idan Adar
  • 44,156
  • 13
  • 50
  • 89
Mika X
  • 15
  • 2
  • 7

1 Answers1

3

If you insist on closing the InAppBrowser when the user navigates somewhere else, you can perhaps try this: Phonegap build - Open external page in InAppBrowser or childbrowser with no toolbar and close it?

// open InAppBrowser w/out the location bar
var ref = window.open('http://myloginapp.com', '_blank', 'location=no');

// attach listener to loadstart
ref.addEventListener('loadstart', function(event) { 
    var urlSuccessPage = "http://myloginapp/success/";
    if (event.url == urlSuccessPage) {
    ref.close();    
    }
});

Edit: I suggest to also review the entire documentation of InAppBrowser and its use of Events. http://docs.phonegap.com/en/3.1.0/cordova_inappbrowser_inappbrowser.md.html

Maybe loadstart is not the right event to use.

That said, the InAppBrowser behaves like a regular webpage; I don't think you can do many 'operations' on, or rather, in it.

Community
  • 1
  • 1
Idan Adar
  • 44,156
  • 13
  • 50
  • 89
  • That request makes sense, imagine a wizard you want the user to go through, and want him to return to the app once navigated the last page. I used a similar approach using Facebook or Twitter share dialog. – Andrea Casaccia Apr 30 '14 at 06:43
  • Okay. I think then that the first solution of using InAppBrowser events should work with some modification depending on his code. – Idan Adar Apr 30 '14 at 06:45
  • Anubis - could you verify that this code could work for that? I am trying to do exactly that same thing you mentioned, but for Twitter! – Mika X May 01 '14 at 01:35
  • Do you control the contents of the page loaded within the InAppBrowser or not? What Exactly are you loading there? – Idan Adar May 01 '14 at 01:37
  • I'm not sure what you mean by control the contents of the page? I basically direct the user to twitter.com (easier than using oauth process) and just have them use twitter within browser, but once the user has logged in (the url becomes the news feed url) I want to redirect the user directly to the post URL, and once posted back to the app, if possible. – Mika X May 01 '14 at 01:57
  • I don't think that's at all similar to what anubis was doing? In twitter's mobile website, a tweet URL is "https://mobile.twitter.com/compose/tweet" After the user submits the tweet, he is redirected again to the homepage, at "https://mobile.twitter.com/" So if you're loading the first URL above, try pointing to the second URL as the "urlSuccessPage" from my code example in the answer. – Idan Adar May 01 '14 at 02:09