Change User Agent in UIWebView

前端 未结 14 1978
孤城傲影
孤城傲影 2020-11-22 17:03

I have a business need to be able to customize the UserAgent for an embedded UIWebView. (For instance, I\'d like the server to respond differently if, say, a user is using

14条回答
  •  眼角桃花
    2020-11-22 17:50

    It should work with an NSMutableURLRequest as Kuso has written.

    NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://www.google.com/"]];
    [urlRequest setValue: @"iPhone" forHTTPHeaderField: @"User-Agent"]; // Or any other User-Agent value.
    

    You'll have to use NSURLConnection to get the responseData. Set the responseData to your UIWebView and the webView should render:

    [webView loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)encodingName baseURL:(NSURL *)baseURL];
    

提交回复
热议问题