[iOS] Cannot load twitter page in UIWebView


#1

Hello,

I have an IOS app that opens the Twitter page inside a UIWebView. This was working great until today when the webview started to throw these errors:

Error Domain=WebKitErrorDomain Code=101 “The URL can’t be shown” UserInfo=0x208c10 {NSErrorFailingURLKey=twitter://user?screen_name=Zariba_Bubbles, NSErrorFailingURLStringKey=twitter://user?screen_name=Zariba_Bubbles, NSLocalizedDescription=The URL can’t be shown}

and:

Error Domain=WebKitErrorDomain Code=102 “Frame load interrupted” UserInfo=0x2d4230 {NSErrorFailingURLKey=twitter://user?screen_name=Zariba_Bubbles, NSErrorFailingURLStringKey=twitter://user?screen_name=Zariba_Bubbles, NSLocalizedDescription=Frame load interrupted}

Obviously when the server detects that I’m trying to open from an UIWebView it redirects the request and tries to open it with the twitter app but the app is not installed! After installing the app the page opens the twitter app, but I don’t want my app to go in background!

Anyone has the same issue? And a fix?


#2

Apparently UIWebView does not identify as mobile safari, and today Twitter started redirecting unidentified user agents to the “twitter://” protocol.

You can get the old behavior by setting the user agent explicitly yourself:

NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@“Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3”, @“UserAgent”, nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

This solution was found here:


#3

Yep we’re getting this issue too. The user-agent fix looks good apart from the width problem mentioned on the SO question.


#4

I can’t see the point of redirecting unidentified user agents to twitter:// protocol. I will try the above mentioned fix, but already I something else. Just catching the error from the web view and opening a safari window.


#5

I like that approach, Nikola. Not a huge fan of masquerading as Mobile Safari with a hard-coded User Agent string. Also, kind of hoping Twitter provides some guidance on this…


#6

All -

Thanks for bringing this to our attention. We’re currently investigating the new redirect behavior and will post an update with guidance soon.

–Sean


#7

Appreciate the update, Sean. Thanks.


#8

Yes the new update is working as expected! Very fast fix! Thank you!


#9

Awesome, it works now! Thanks for the fix.


#10

This issue is now resolved and you should be able to load twitter.com URLs in UIWebViews.

Update: please note that the version of the mobile site displayed for webviews is the older, lighter-weight version. The modern version is much slower, especially when you’re only loading a single page.

If you wish to show your users the newer version (as seen in Mobile Safari), you can override the User-Agent checks that are performed on our side with the following, as originally posted by @_kims:

NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3", @"UserAgent", nil]; [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

#11

Thanks very much! Appreciate the quick resolution.


#12

Glad to see this fixed! Is there a best practice for linking to twitter.com in UIWebViews or other embedded mobile browsers?

We currently reference http://m.twitter.com/<use_name>, which broke when this redirect issue came up a few weeks back (Twitter Support #4735641). Should we instead reference http://mobile.twitter.com/<use_name> or simply http://twitter.com/<use_name>?


#13

Ok, twitter.com is now loading in UIWebView, but it loads the old (bluish) UI, not the new one.
Is that the expected behavior?


#14

You should use http://mobile.twitter.com/ for any mobile integrations.

If you use http://twitter.com, we’ll redirect the user to the mobile site, at the expense of another few redirects for the client.

http://m.twitter.com is the lightweight mobile site that we still use for some feature phones. In the event that you point one of these devices at mobile.twitter.com, you’ll end up on m.twitter.com. But, if you point a modern device directly at m.twitter.com, we do not guarantee that the user will end up on mobile.twitter.com.


#15

Yes, I’ve confirmed that this is the expected behavior. This version of the profile page is very lightweight and loads quickly in the view, unlike the newest site.


#16

Users are complaining because they want the newest site anyway. Any chance to give the option to developers to pick one version vs. the other? Maybe two different addresses or some parameter?


#17

You can override the UIWebView User-Agent handling by explicitly setting the UserAgent preference, as @_kims posted.


#18

Hi Friends,

I have update the user agent as per mentioned above in my app but still UIwebview does not load twitter page. I have use the iOS 7 sdk with updated iOS 7.1.1. Also when i tried to open mobile.twitter.com in safari still shows same issue - "There’s been problem loading the page. Please refresh and try again. "

It will be appreciate If you could provide me solution on this issue.