Advertise here

Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

UIWebView: Forcing non-mobile web pages possible?

rakoltarakolta Posts: 3New Users
edited May 2009 in iOS SDK Development
When specifying a URL using UIWebView, is there a way to force web sites (like to provide the original (non-mobile) version of the web pages?
Post edited by rakolta on


  • RickMaddyRickMaddy Posts: 2,122New Users
    edited January 2009
    If you set the user agent header to something that makes it appear the request is coming from a desktop browser instead of a mobile browser you will most likely get what you want.
  • rakoltarakolta Posts: 3New Users
    edited January 2009

    I searched the web on how to do this, no luck.

    I found examples that use:

    [webView setCustomUserAgent:@"...

    but I have no idea how to get Iphone SDK to access this method.

    Any more hints?
  • RickMaddyRickMaddy Posts: 2,122New Users
    edited January 2009
    Setup a UIWebViewDelegate and in the method 'webView:shouldStartLoadWithRequest:navigationType:' you can cast the NSURLRequest as an NSMutableURLRequest. Once you have that you can do:
    [mutableRequest setValue:@"some appropriate user agent string" forHTTPHeaderField:@"User-Agent"];

    I'm not 100% positive about being able to cast the request to a mutable request. I thought I was doing that in one of my apps but now I can't find the code. If the casting fails at runtime, then you will need to create a new mutable request based on the original request.
  • rakoltarakolta Posts: 3New Users
    edited January 2009
    I tried the following:

    NSString *urlString = [NSString stringWithFormat:@""];
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setValue:@Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1) forHTTPHeaderField@"User-Agent"];
    [webView loadRequest:request];

    This loads the URL, but the changing of the "User-Agent" seems to have no effect. Perhaps the Iphone simulator is overriding any changes to this field.

    Anyway, thanks for getting me in the right direction.
  • kusolitokusolito Posts: 4New Users
    edited February 2009

    I tried below, but didn't work. Can anyone help?

    [myWebView loadRequest:[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@""]]];

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    [(NSMutableURLRequest *) request setValue:@Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5G77 Safari/525.20 forHTTPHeaderField:@User-Agent];
    return true;
  • BungleroBunglero Posts: 2New Users
    edited May 2009
    Here is what I did which worked for me. It does not seem to be possible to overwrite the User-Agent from the NSURLRequest, as it seems that this header gets written over when the request goes out. However, you are able to add custom defined header key/value pairs. These key/values would not show up in the normal server request logs, but they can be tapped, for instance, with PHP:

    In the Obj-C:
    NSString *urlString = [NSString stringWithFormat:@""];
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setValue:@"My-Special-Agent/1.0" forHTTPHeaderField:@"My-User-Agent"];
    [webView loadRequest:request];

    ...then in PHP (using Apache server):
    echo "<pre>";
    echo "</pre>";	

    That should give you a list of all the header key/values including your custom ones. I'm not an Apache expert, but it would seem to me that there should be a way of logging this custom header info in the normal request logs, if that's where you need them.
Sign In or Register to comment.