更改UIWebView中的用户代理(iPhone SDK)

我有一个业务需要能够为embedded式UIWebView自定义UserAgent。 (例如,如果用户使用一个版本的应用程序,而另一个版本的应用程序则需要服务器做出不同的响应。)

是否有可能在现有的iPhone SDK的UIWebView控件中自定义UserAgent,就像在Windows应用程序中embeddedIE浏览器一样?

随着iOS 5的变化,我推荐以下方法,最初从这个StackOverflow问题: UIWebView iOS5更改用户代理正如在下面的答案中指出的。 在该页面的评论中,它似乎在4.3和更早的版本中工作。

当您的应用程序启动时,通过运行以下代码更改“UserAgent”默认值:

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

如果你需要在4.3 / 5.0之前版本的iOS中工作的方法,请参阅这篇文章以前的编辑。 请注意,由于广泛的编辑,本页面上的以下评论/其他答案可能没有意义。 毕竟,这是一个四岁的问题。 😉

它应该像Kuso写的一样使用NSMutableURLRequest。

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

你将不得不使用NSURLConnection来获取responseData。 设置responseData到你的UIWebView和webView应该呈现:

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

我也有这个问题,并尝试了所有的方法。 我发现只有这个方法可以工作(iOS 5.x): UIWebView iOS5更改用户代理

原则是将用户代理永久设置在用户设置中。 这工作; Webview发送给定的标题。 只有两行代码:

 NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Mozilla/Whatever version 913.6.beta", @"UserAgent", nil]; [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary]; 

在可变请求中设置User-Agent或User_Agent,或者通过swizzling覆盖NSHttpRequest中的setValue, – 我尝试了所有这些,并用wireshark控制了结果,而这些都不起作用,因为Webview仍然使用用户代理值从用户默认情况下,不pipe你尝试在NSHttpRequest中设置什么。

Swift非常简单。 只需将以下内容放入您的App Delegate中。

 UserDefaults.standard.register(defaults: ["UserAgent" : "Custom Agent"]) 

如果你想附加到现有的代理string,那么:

 let userAgent = UIWebView().stringByEvaluatingJavaScript(from: "navigator.userAgent")! + " Custom Agent" UserDefaults.standard.register(defaults: ["UserAgent" : userAgent]) 

注意:您可能需要卸载并重新安装应用程序以避免附加到现有的代理string。

实际上,将任何头字段添加到shouldStartLoadWithRequest中的NSURLRequest参数似乎工作,因为请求响应setValue:ForHTTPHeaderField – 但它实际上不工作 – 请求发送不带标题。

所以我在shouldStartLoadWithRequest中使用了这个解决方法,它只是将给定的请求复制到一个新的可变请求中,并重新加载它。 这实际上修改了发送出的报头。

 if ( [request valueForHTTPHeaderField:@"MyUserAgent"] == nil ) { NSMutableURLRequest *modRequest = [request mutableCopyWithZone:NULL]; [modRequest setValue:@"myagent" forHTTPHeaderField:@"MyUserAgent"]; [webViewArgument loadRequest:modRequest]; return NO; } 

不幸的是,这仍然不允许覆盖用户代理http头,这显然被苹果覆盖。 我想重写它,你将不得不自己pipe理一个NSURLConnection。

使用@“User_Agent”只会导致自定义标题出现在GET请求中。

User_agent:Foobar / 1.0 \ r \ n
用户代理:Mozilla / 5.0(iPhone; U;像Mac OS X的CPU iPhone OS 3_1_2; en-us)AppleWebKit / 528.18(KHTML,如Gecko)Mobile / 7D11 \ r \ n

以上就是解密后的HTTP数据包中出现的情况,从本质上证实了Sfjava从该论坛引用的内容。 有趣的是,“User-Agent”变成了“User_agent”。

把所有这一切都解决了,

 - (void)viewDidLoad { NSURL *url = [NSURL URLWithString: @"http://www.amazon.com"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setValue:@"Foobar/1.0" forHTTPHeaderField:@"User-Agent"]; [webView loadRequest:request]; } 

感谢大家。

这个解决scheme似乎被认为是一个非常聪明的方式来做到这一点

改变最头换uiwebkit-HTTP的请求

它使用方法Swizzling,您可以在CocoaDev页面上了解更多关于它的信息

看一看!

通过将Louis St-Amour和NSUserDefaults+UnRegisterDefaults类别的回答合并到此问题/答案中 ,您可以在运行应用程序的任何时候使用以下方法启动和停止用户代理欺骗:

 #define kUserAgentKey @"UserAgent" - (void)startSpoofingUserAgent:(NSString *)userAgent { [[NSUserDefaults standardUserDefaults] registerDefaults:@{ kUserAgentKey : userAgent }]; } - (void)stopSpoofingUserAgent { [[NSUserDefaults standardUserDefaults] unregisterDefaultForKey:kUserAgentKey]; } 

我面临同样的问题。 我想给用户代理添加一些信息,还需要保留webview的原始用户代理。 我使用下面的代码解决了它:

  //get the original user-agent of webview UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero]; NSString *oldAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]; NSLog(@"old agent :%@", oldAgent); //add my info to the new agent NSString *newAgent = [oldAgent stringByAppendingString:@" Jiecao/2.4.7 ch_appstore"]; NSLog(@"new agent :%@", newAgent); //regist the new agent NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:newAgent, @"UserAgent", nil]; [[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary]; 

在实例化webview之前使用它。

在AppDelegate.m中试试这个

 + (void)initialize { // Set user agent (the only problem is that we can't modify the User-Agent later in the program) // iOS 5.1 NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:@”Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B176 Safari/7534.48.3”, @”UserAgent”, nil]; [[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary]; } 

我发现的唯一问题只是更改用户代理

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys: @"Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5", @"UserAgent", nil]; [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary]; } 

要将自定义内容添加到当前UserAgent值,请执行以下操作:

1 – 从NEW WEBVIEW获取用户代理值

2 – 添加自定义内容

3 – 用密钥UserAgent将新值保存在字典中

4 – 将字典保存在standardUserDefaults中。

看下面的例子:

 NSString *userAgentP1 = [[[UIWebView alloc] init] stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]; NSString *userAgentP2 = @"My_custom_value"; NSString *userAgent = [NSString stringWithFormat:@"%@ %@", userAgentP1, userAgentP2]; NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:userAgent, @"UserAgent", nil]; [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];