Ok, here is the scenario: I am creating an iPad application that will display a UIWebView, width = 320 pixels (or dots now). This will cause the widget to appear inside the application.
I had to change the webview user agent to display the mobileversion version instead of the desktop version, doing this on self.viewDidLoad.
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];
I also set the webview frame to 320 pixels wide and the settings are scaled by PageToFit to YES.
Ok, but here's the problem: the webview displays the mobile version, but the mobile version of TABLET, the one that is likely to appear on Honeycombs tablets. This is a kind of gigantic web application for the iPhone with a size of approximately 600 pixels. The UIwebView frame has a width of 320 pixels, but the content is much wider.
I don’t understand what could happen because the user agent is configured on Mobile Safari on the iPhone. Any idea on how to make this widescreen UIWebView with 320 pixels shows the same version that you get when you type mobile .twitter.com on iPhone Mobile Safari?
EDIT: Well, the problems seem to be related to the fact that twitter webapp uses “device width” to determine which web image to show to the user.
I found this solution: displaying a Wiki page in a UIWebView in a UIPopoverController
This is a kind of work:
-(void)webViewDidFinishLoad:(UIWebView *)webView{
if (hasLoaded == NO){
NSString *webHTML = [NSString stringWithContentsOfURL:self.webView.request.URL encoding:NSUTF8StringEncoding error:NULL];
NSRange range = [webHTML rangeOfString:@"device-width"];
if ((range.location!=NSNotFound)&&(range.length != 0)) {
webHTML = [webHTML stringByReplacingOccurrencesOfString:@"device-width" withString:@"320" options:0 range:range];
[self.webView loadHTMLString:webHTML baseURL:[NSURL URLWithString:@"http://mobile.twitter.com"]];
hasLoaded = YES;
}
}
It changes the device - with a code of 320, which is perfect, BUT ... when the webview restarts (with a new code), I get an OLD (very old) Twitter web application, and not the current one. Any idea on how to fix this?