UIWebView load desktop version of website not Mobile version

ios, iphone, uiwebview

Solution

It deppends on the web, but you can try to change the user agent to make the server think it's a desktop browser

To change the "UserAgent" default value run this code when your app starts:

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0", @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];  

Problem

There is a button on ViewController A, that when pressed should take me to ViewControllerB which has a UIWebView. That web view should load the desktop version of a website. These are the steps I take: ViewController A - Touch button ViewController B - The UIWebView loads the mobile version of the website instead of desktop version. NavigationController - Touch the back button -> takes us back to ViewController A ViewController A - Touch the button again ViewController B - UIWebView loads the desktop version as desired. The first time (Step 2), the UIWebView does not load the correct URL and second time (Step 5) it loads the correct URL. VIEW A- ``` -(IBAction)ButtonPressed:(id)sender{ HomeAppDelegate *myDelegate = (HomeAppDelegate *)[[UIApplication sharedApplication]delegate]; NSString *titleCaption;WebPageViewController *chosenViewController = [[WebPageViewController alloc] initWithNibName:@"WebPageViewController" bundle:nil]; titleCaption = @"Web PAGE"; chosenViewController.url = [NSURL URLWithString:@"http://www.mydesktopwebsite.com"]; [self.navigationController pushViewController:chosenViewController animated:YES]; myDelegate.navBar.topItem.title = titleCaption; [chosenViewController release]; } ``` VIEW B - WebPageViewController.h ``` @property (retain, nonatomic) IBOutlet UIWebView *WebView; @property (retain, nonatomic) NSURL *url; ``` WebPageViewController.m ``` @synthesize url; - (void)viewDidLoad{ [super viewDidLoad]; [self.WebView loadRequest:[NSURLRequest requestWithURL:self.url]];} ``` (Edit note from jcesar: You edited my answer, but I think you wanted to edit your question, so I add the code you put in my answer) I was using following code which obviously wrong. ``` NSDictionary *dictionary = @{ @"UserAgent" : @"Safari iOS 5.1 - iPhone"}; [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary]; ```

Original source

Related problems