Objective c UIWebView iOS5更改用户代理

Objective c UIWebView iOS5更改用户代理,objective-c,ios,cocoa-touch,uiwebview,Objective C,Ios,Cocoa Touch,Uiwebview,如何在iOS 5中更改UIWebView的用户代理 到目前为止我所做的: 使用委托回调,截获NSURLRequest,创建一个新的url请求,并将其用户代理设置为我想要的任何内容,然后下载数据并用“loadData:MIMEType:…”重新加载UIWebView 问题: 这会导致无限递归,在这里我加载数据,然后调用委托,intern调用委托 以下是委托方法: - (BOOL)webView:(UIWebView *)aWebView shouldStartLoadWithRequest:(NS

如何在iOS 5中更改UIWebView的用户代理

到目前为止我所做的: 使用委托回调,截获NSURLRequest,创建一个新的url请求,并将其用户代理设置为我想要的任何内容,然后下载数据并用“loadData:MIMEType:…”重新加载UIWebView

问题: 这会导致无限递归,在这里我加载数据,然后调用委托,intern调用委托

以下是委托方法:

- (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;
}
在应用程序启动时运行此代码一次,以更改“UserAgent”默认值:

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Your user agent", @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];  

编辑:我很成功地使用了它,但想添加更多细节。要获取用户代理,您可以启用“开发人员”菜单,设置用户代理,然后连接到此站点以将其打印出来:。同样,你可以使用任何类型的移动设备进行连接,也可以通过这种方式进行连接。顺便说一句,在iOS7+

中,当您发送消息
[aWebView loadData:MIMEType:textcencodingname:baseURL:

然后,
aWebView shouldStartLoadWithRequest:
将被再次调用,然后再次调用-这就是为什么会得到无限递归

您应该限制调用
dispatch\u async()
块,例如使用一些常规URL:

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


    if ([[[request URL] absoluteString] isEqualToString:@"http://yourdomain.com/?local=true"]) {
        return YES;
    }


    ...

    dispatch_async(...
            [aWebView loadData:data 
                      MIMEType:[response MIMEType] 
              textEncodingName:[response textEncodingName] 
                       baseURL:[NSURL URLWithString:@"http://yourdomain.com/?local=true"]];

    );
    return NO;
}

Swift中,使用此选项设置UserAgent

func setUserAgent(){

    var userAgent = NSDictionary(objectsAndKeys:  "YourUserAgentName","UserAgent")

    NSUserDefaults.standardUserDefaults().registerDefaults(userAgent as [NSObject : AnyObject])

}
用这个来测试,

println(WebView.stringByEvaluatingJavaScriptFromString("navigator.userAgent"));

这是正确的。关于在shouldStartLoadWebRequest中设置它的原始帖子的问题是:该方法只在顶级请求上调用,因此所有内部请求都会得到旧的用户代理。我使用fiddler2代理验证了这一点。在standardUserDefaults中设置它会导致所有请求获得新的用户代理。谢谢上面的代码!最后真正有效的东西。。。。非常感谢马丁。。。。我甚至在github上发现了一些令人惊讶的组件,它们太复杂了,无法使用。。。您的解决方案简单且易于实施…同样适用于iOS 4.3.3。任何能在4.0上测试它的人?都很棒!我必须在phonegap应用程序的AppDelegate.mm中的+(void)initialize{}方法中设置它。这非常有效:+(void)initialize{NSDictionary*dictionary=@{@“UserAgent”:@“Safari iOS 5.1-iPhone”};[[NSUserDefaults standardUserDefaults]registerDefaults:dictionary];}请共享setCurrentReqest方法。可能的重复项