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

I am trying to insert a small webview (320x480) inside my iPad application to simulate a small “iPhone screen” displaying a mobile twitter. But every time uiwebview receives NSUrlRequest for downloading http://mobile.twitter.com , my application automatically breaks off the screen and iOS opens Twitter for iPad.

Is there any way to change this behavior?

That'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"]]]; 

Edition:

OK, I found a 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 before a new problem: mobile.twitter.com insists on adapting to the size of the iPad screen instead of the specified width of 320 pixels.

+6
source share
2 answers

If you do this like this, you should not 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 possible with this line of code (redirection):

 [[UIApplication sharedApplication] openURL:url]; 
+1
source

I also noticed this new behavior in UIWebView in a number of applications. It just started from today, because yesterday all these applications worked fine.

If you try to connect instead to https://mobile.twitter.com (note that the URL is "https") in UIWebView, you will be signed in to Twitter but after entering the credentials, the site will try to redirect your application to twitter: // timeline after logging in. If you do not have the official Twitter mobile application installed on your device, you will not be able to view the Twitter mobile site in UIWebView. If you have your own Twitter application installed, you will be redirected to this application instead of viewing the mobile site in UIWebView.

The strange thing is that if you try to do the same in Mobile Safari, you won’t get this redirect to your Twitter native mobile app.

This is some kind of new restriction on the part of Twitter, as it will violate a number of applications that will access the Twitter mobile site in UIWebView.

I'm not sure other users experienced the same new behavior?

PS: Just checked the Twitter developer support forum, and did someone else encounter the same problem?

+1
source

Source: https://habr.com/ru/post/910339/


All Articles