Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems loading mobile.twitter in webview

When I try to load the following urls in a Webview all i get is black twitter background with loading spinner. The page is loaded, as WebViewClient.onPageFinished is called. However the page loads ok in the standard Android browser.

https://twitter.com/#!/scottyab or https://mobile.twitter.com/#!/scottyab

I'm thinking Twitter changed their mobile website as this worked a month or so ago. Anyone else experiencing this?

Updated: javascript enabled mWebView.getSettings().setJavaScriptEnabled(true);

like image 937
scottyab Avatar asked Jul 28 '11 09:07

scottyab


2 Answers

Those are indeed dirty hacks. Changing the User Agent is really a nasty solution and should never be done. When loading twitter.com in a webview, you'd better try the piece of code given there : https://stackoverflow.com/a/6625418/162178

For lazy clickers I'll give it here :

webView.getSettings().setDomStorageEnabled(true);

All credits goes to gregm, who gave this one :)

Happy coding !

Edit: Just a little update to justify this choice, User Agent are meant to give the visited site the info about who's the client. If one day Twitter makes special changes dedicated to Android, they will definitely use the User Agent to achieve that. If you tell them your an iPhone or whatever you might never get redirected or more simply never get the css intended specially for Android.

And in a more political matter, if everyone changes it's User Agent, site statistics will be wrong and they might never see there are a lot of Androids coming to their website. ^^ (To maybe consider engaging the proper updates to their website). And this whole thing is not only for Twitter web clients. So be nice.

Benjamin's answers here seems quite good to (using Java's Reflection to make it backwards compatible if I'm correct)

So again don't change the User Agent it is very bad for your app and the web health. And should simply be banned from any code out there. Thanks :)

like image 143
MrBuBBLs Avatar answered Nov 16 '22 03:11

MrBuBBLs


Fixed the loading issue by hardcoding the user agent to iPhone's user agent (I found it worked on the iPhone version of the app)

mWebView.getSettings().setUserAgentString("Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3");

Need to wash my hands after this a dirty hack.

like image 44
scottyab Avatar answered Nov 16 '22 01:11

scottyab