2

How can I get a UIWebView to load non-mobile sites?

Currently I'm using this:

 NSMutableURLRequest *request = [NSURL URLWithString:websiteLink];
 [request setValue:
      [NSString stringWithFormat:@"%@ Safari/528.16", [request valueForHTTPHeaderField:@"User-Agent"]]  
      forHTTPHeaderField:@"User_Agent"];
 [webView loadRequest:request];

but the app crashes when loading the web view.

Any help would be greatly appreciated.

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
Jonathan Gurebo
  • 285
  • 1
  • 6
  • 19
  • 1
    Basically you need to change the user-agent: http://stackoverflow.com/questions/8487581/uiwebview-ios5-changing-user-agent/8666438#8666438 – Lefteris Jan 14 '13 at 15:17
  • what error are you getting when the app terminates? – propstm Jan 14 '13 at 15:17
  • He is doing it wrong. He needs to change the user-agent the way I showed in the provided link – Lefteris Jan 14 '13 at 15:18
  • This implies the app works with some URLs and not with others? Apps do not terminate because you load a non-mobile page, so there is some other issue. My 2 cents - changing the user agent is unlikely to "fix" this. After you correct the crash bug, you can try setting [self.myWebView setScalesPageToFit:YES]; to make non-mobile pages look better. – Dave Jan 14 '13 at 15:28
  • So i just need to create a NSDictionary? , @Dave i have done that in storyboard inspector view "SetScalesPageToFit" do i need the code anyway? – Jonathan Gurebo Jan 14 '13 at 15:29
  • post the console log and the exact error message. – sergio Jan 14 '13 at 15:33
  • @Jon, nope, setting in Storyboard works just as well. – Dave Jan 14 '13 at 15:39
  • Error Message: http://jonathangurebo.tumblr.com/post/40521720109/error-message – Jonathan Gurebo Jan 14 '13 at 15:44
  • sorry it was to many characters – Jonathan Gurebo Jan 14 '13 at 15:54
  • @JonathanGurebo You should edit your post and add it to that, not as a comment. **That** is your crash. – Kitsune Jan 15 '13 at 16:19

3 Answers3

0

You have to specify the user-agent in your request:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setValue:@"<Your-User-Agent>" forHTTPHeaderField:@"User-Agent"];

Where can be something like:

[NSString stringWithFormat:@"%@ Safari/<version number>", [request valueForHTTPHeaderField:@"User-Agent"]

EDIT:

My User-Agent string is like:

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/536.26.17 (KHTML, like Gecko) Version/6.0.2 Safari/536.26.17

Please note that webservers usually cache informations such as your browser information, so if you have visited the site already the code above might not work.

Attila H
  • 3,616
  • 2
  • 24
  • 37
  • the app will not terminate when using a little bit of your code. but instead i comming to a non-touch-mobile-website. i'm using: NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url34]; [request setValue:[NSString stringWithFormat:@"%@ Mozilla/5.0", [request valueForHTTPHeaderField:@""]] forHTTPHeaderField:@"User_Agent"]; – Jonathan Gurebo Jan 14 '13 at 15:52
  • but what should i set instead of ? what User Agent should i use? – Jonathan Gurebo Jan 14 '13 at 15:53
  • Take a look at the documentation. http://developer.apple.com/library/safari/documentation/appleapplications/reference/safariwebcontent/OptimizingforSafarioniPhone/OptimizingforSafarioniPhone.html#//apple_ref/doc/uid/TP40006517-SW3 – Attila H Jan 14 '13 at 16:04
  • I tested some of the code in the document but i didn't find a solution, Please, can you give me the exactly code i should use? i'm a beginner. – Jonathan Gurebo Jan 14 '13 at 22:01
  • its not working, i use: NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url34]; [request setValue:@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/536.26.17 (KHTML, like Gecko) Version/6.0.2 Safari/536.26.17" forHTTPHeaderField:@"User-Agent"]; [webView loadRequest:request]; – Jonathan Gurebo Jan 15 '13 at 16:32
0

According to the error (in the picture you added), you're calling a selector that doesn't exist. Specifically, calling valueForHTTPHeaderField: on request (which is an NSURL) is invalid because NSURL doesn't respond to that method.

Kitsune
  • 9,101
  • 2
  • 25
  • 24
  • its not working, i use: NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url34]; [request setValue:@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/536.26.17 (KHTML, like Gecko) Version/6.0.2 Safari/536.26.17" forHTTPHeaderField:@"User-Agent"]; [webView loadRequest:request]; – Jonathan Gurebo Jan 20 '13 at 19:21
0

Jonathan,

It's been a few days, but in case you are still looking for the answer to this - the problem is right here:

NSMutableURLRequest *request = [NSURL URLWithString:websiteLink]; // <-- BUG

Your are stuffing an NSURL into a MutableURLRequst. Oops!

This is what you intended to do:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:websiteLink]];

Good luck.

Dave
  • 7,552
  • 4
  • 22
  • 26
  • its not working, i use: NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url34]; [request setValue:@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/536.26.17 (KHTML, like Gecko) Version/6.0.2 Safari/536.26.17" forHTTPHeaderField:@"User-Agent"]; [webView loadRequest:request]; – Jonathan Gurebo Jan 20 '13 at 19:22
  • Fixing that line should absolutely prevent your crash. You should not need to change your user-agent header, but if you insist the best way is to change it in userDefaults. See: http://stackoverflow.com/questions/8487581/uiwebview-ios5-changing-user-agent Just setting it as you are only changes it for the 1st request, and not all the subsequent requests that the webpage will make. – Dave Jan 20 '13 at 21:03