views:

1894

answers:

2

Is there any way to spoof the user agent on Safari on the iPhone?

So for example, you would create an application on the iPhone that has the embedded Safari browser, however any website the user visits with this browser wouldn't know you were on Safari on the iPhone, it would think you are on something like Safari on a PC, or even IE/FireFox.

Thanks

+3  A: 

Hi,

Yes I think you could change this. It would require a bit of a work around to get it working.

  1. You would need to manually manage all requests. By making your own data requests. In this data request you can add a HTTPheader for User-Agent which will override the default headers.

    NSMutableURLRequest* urlRequest = [[[NSMutableURLRequest alloc] initWithURL:requestURL] autorelease];

    [urlRequest setHTTPMethod: @"POST"];
    [urlRequest setHTTPBody: [nvpString  dataUsingEncoding:NSUTF8StringEncoding]];
    [urlRequest addValue:@"Your+User+Agent+String" forHTTPHeaderField:@"User-Agent"];
    receivedData = [[NSMutableData alloc] retain];
    [receivedData setLength:0];
    
    
    [NSURLConnection connectionWithRequest: urlRequest delegate: self];
    
  2. If you embed the Safari Web Browser in your app you can subscribe to its delegate methods. One of them will notify your application that safari would like to load a URL, this is where you catch this load and get the data your self.

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    

    now you put your code in here to do the data load.

  3. Once the data has loaded. Give it the data string back to the webView. I have set "baseURL:nil" but you might have to correctly set this to maybe the correct domain for this app.

    [webView loadHTMLString:newString baseURL:nil]

John Ballinger
A: 

john are you sure this trick works for overriding User-Agent? I tried it with NSURL connection and asynchronous load (without webview) and it did not work for me. didFailWithError got called - error code was bad url!

So, are you sure it works with webview? Before changing my code to use webview I wanted to double check

Thanks for your time Kumar

Santosh