我怎样才能在iOS 5中更改UIWebView的用户代理?
到目前为止我所做的事情: 使用委托回调函数,拦截NSURLRequest,创建一个新的URL请求并设置其用户代理为我想要的任何内容,然后下载数据并使用"loadData:MIMEType:..."重新加载UIWebView。
问题: 这会导致无限递归,其中我加载数据,调用委托回调函数,进而又调用了委托...
这是委托方法:
- (BOOL)webView:(UIWebView *)aWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
dispatch_async(kBgQueue, ^{
NSURLResponse *response = nil;
NSMutableURLRequest *newRequest = [NSMutableURLRequest requestWithURL:[request URL]];
NSDictionary *headers = [NSDictionary dictionaryWithObject:
@"custom_test_agent" forKey:@"User-Agent"];
[newRequest setAllHTTPHeaderFields:headers];
[self setCurrentReqest:newRequest];
NSData *data = [NSURLConnection sendSynchronousRequest:newRequest
returningResponse:&response
error:nil];
dispatch_sync(dispatch_get_main_queue(), ^{
[webView loadData:data
MIMEType:[response MIMEType]
textEncodingName:[response textEncodingName]
baseURL:[request URL]];
});
});
return YES;
}