【问题标题】:UIWebView iOS5 changing user-agentUIWebView iOS5 改变用户代理
【发布时间】:2012-01-19 05:31:11
【问题描述】:

如何在 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;
}

【问题讨论】:

标签: objective-c ios cocoa-touch uiwebview


【解决方案1】:

通过在您的应用启动时运行一次此代码来更改“UserAgent”默认值:

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

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

【讨论】:

  • 工作正常。关于在 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]; }
【解决方案2】:

Swift 中使用它来设置 UserAgent

func setUserAgent(){

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

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

}

用这个来测试,

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

【讨论】:

    【解决方案3】:

    当你发送消息[aWebView loadData:MIMEType:textEncodingName:baseURL:]

    然后aWebView shouldStartLoadWithRequest: 将被再次调用,然后再次调用 - 这就是你获得无限递归的原因

    您应该限制调用 dispatch_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;
    }
    

    【讨论】:

      猜你喜欢
      • 2010-10-03
      • 2012-12-14
      • 1970-01-01
      • 2011-03-18
      • 1970-01-01
      • 2011-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多