Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Requesting http://mobile.twitter.com on UIWebView

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.

like image 399
JulianoRossi Avatar asked Mar 09 '12 10:03

JulianoRossi


1 Answers

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];
like image 92
Eugene Avatar answered Nov 11 '22 06:11

Eugene