Brian Miller
Brian Miller

Reputation: 71

Return User to Android App from Browser

I am building a Twitter app that opens a webpage to allow someone to authorize my app. The problem occurs once the user has authenticated the site. It returns me back to my url, from which I'd like to close the browser window to return to my native app. I've tried the following, but the browser window remains open:

    <script>
      window.close();
      window.self.close();
    </script>

The users would currently have to go back to the home screen and find my app again.

Is there javascript that would return the user to my app?

Thanks!

Upvotes: 1

Views: 741

Answers (1)

tiguchi
tiguchi

Reputation: 5410

You should use a WebView in your own app for that and not use the browser app for logging in, because you cannot close it.

Create a login Activity with a WebView in it that loads the Twitter login page. Implement a custom WebViewClient for that WebView, override the onPageFinished method and check whether the URL parameter equals your login redirection target URL. Parse out the access token from the URL or page (I do not know the exact technical details here, sorry).

Then you can store the access token in either an application private database or preferences file, and finally finish the login Activity which will bring you back to the previous one.

On a side note: you should specify the type="text/javascript" argument in <script> tags otherwise these tags might get ignored depending on the browser.

Upvotes: 3

Related Questions