6
votes

I've been trying to insert a little webview (320x480) inside my iPad app, to simulate a little "iPhone screen" displaying mobile twitter. But, every time uiwebview gets an NSUrlRequest to load http://mobile.twitter.com, my app automatically gets ripped off the screen, and iOS opens Twitter for iPad.

Is there any way to change that behavior?

Here's what I'm doing:

UIWebView *viewDoTwitter = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.bounds.size.height)];
viewDoTwitter.autoresizingMask = UIViewAutoresizingFlexibleHeight;
viewDoTwitter.scalesPageToFit = YES;

 [rootView insertSubview:viewDoTwitter atIndex:0];
 [viewDoTwitter loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://mobile.twitter.com"]]];

EDITED:

OK, I found the solution, here: http://www.mphweb.com/en/blog/easily-set-user-agent-uiwebview

 UIWebView *viewDoTwitter = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.bounds.size.height)];
 viewDoTwitter.autoresizingMask = UIViewAutoresizingFlexibleHeight;
 viewDoTwitter.scalesPageToFit = YES;

 NSDictionary *dictionnary = [[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:dictionnary];

 [rootView insertSubview:viewDoTwitter atIndex:0];

 [viewDoTwitter loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.twitter.com"]]];

But, now facing a new problem: mobile.twitter.com insists to adapt to the size of the iPad screen, instead of the 320 pixels width I specified.

2
Can you post you UIWebView code please?Mick MacCallum
You want to only display twitter web page in this view or make it usable for the user or something else?Artur Ozierański
Yes, It needs to be usable for the user.JulianoRossi

2 Answers

1
votes

If you are doing it like this, you shouldn't be redirected

- (void)viewDidLoad {
  [super viewDidLoad];
  NSURL *url = [NSURL URLWithString:@"http://mobile.twitter.com"];
  NSURLRequest *request = [NSURLRequest requestWithURL:url];
  [webView loadRequest:request];
}

The behavior you described is achievable by this line of code (redirects):

[[UIApplication sharedApplication] openURL:url];
1
votes

I've also noticed this new behaviour in UIWebView in a number of apps. This has only started happening since today as yesterday all these apps worked fine.

If you try connecting instead to https://mobile.twitter.com (note the url is "https" )in UIWebView you'll get to the Twitter login screen but after entering the credentials the site tries to redirect your app to twitter://timeline after logging in. If you don't have the official Twitter native mobile app installed on the device you cannot browse the Twitter mobile site in UIWebView. If you have the Twitter native app installed you'll be redirected to that app instead of browsing the mobile site in UIWebView.

The strange thing is that if you try the same thing in Mobile Safari you don't get this redirection behaviour to the native Twitter mobile app.

Is this some sort of new restriction from Twitter's side as this will break a number of apps which access the mobile Twitter site within a UIWebView.

I'm not sure if other users have experienced this same new behaviour??

PS: Just checked the Twitter developer's support discussion board and someone else is experiencing the same issue??