0

I'm making a simple UIWebView iOS app. In it, the website I want to display is displaying as the mobile version, when I want it to display as the full version.

I have this code under viewDidLoad:

NSString *fullURL = @"www.facebook.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// [request setValue:@"Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3" forHTTPHeaderField:@"User-Agent"];
[self.webView loadRequest: request];

The commented out code I have found makes the website display as the mobile version. Does anyone know how I could change commented line to be the desktop version? When the commented line stays commented, it still runs as the mobile version.

Thanks that would be really helpful!

Girish
  • 4,692
  • 4
  • 35
  • 55
falky
  • 589
  • 2
  • 11
  • 27

1 Answers1

1

set a desktop user agent for HTTP requests: e.g. '@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2";'

BUT that doesnt work for a webview because the webview doesn't use the agent set for the request. Instead, it uses the agent from NSUserDefaults.

so:

NSString *secretagent = @"%%MyUserAgent%%"; //the one you chose
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:secretAgent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

to get this to take effect, do it before you load the UIWebview!

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • the user agent provided was taken from OSX 10.6.8, safari 5. it is old. but google should help you find better strings – Daij-Djan Jun 18 '13 at 08:04
  • No that's ok. I have no idea what they are nor what they do so I'll do a bit of research into them now! Thank you! – falky Jun 18 '13 at 08:58
  • Nah it's still not working. I've tried many different one's from that site (Thanks @JacobOscarson). Do you have any ideas why @Daij-Djan? My current code is: NSString *userAgent = @"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36"; NSString *fullURL = @"http://www.facebook.com"; NSURL *url = [NSURL URLWithString:fullURL]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setValue:userAgent forHTTPHeaderField:@"User-Agent"]; [self.webView loadRequest: request]; – falky Jun 19 '13 at 07:58
  • http://stackoverflow.com/questions/13247305/setting-ios-useragent-via-nsuserdefaults-works-half-the-time – Daij-Djan Jun 19 '13 at 18:51