用户代理在 NSMutableURLRequest 中没有改变 [英] User-Agent not changing in NSMutableURLRequest
问题描述
我知道这可能有几个问题,但是当我尝试为我的 UIWebView 设置新的用户代理"时遇到了一个问题.这是我现在正在做的:
I know there may be several issues about this, but i´m facing a problem when i try to set a new "user agent" for my UIWebView. Here is what i am doing right now:
NSURL *myUrl = [NSURL URLWithString:auxUrl];
NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL:myUrl];
NSMutableString *auxUserAgent = [[NSMutableString alloc] initWithString:[myRequest valueForHTTPHeaderField: @"User-Agent"]];
[auxUserAgent appendString:@"(iOS)"];
[myRequest setValue:auxUserAgent forHTTPHeaderField:@"User-Agent"];
NSLog(@"UA : %@",[myRequest valueForHTTPHeaderField: @"User-Agent"]);
我正在尝试将一些文本附加到实际的用户代理中,并且在 XCode 控制台中它确实显示了新的用户代理值.
I am trying to append some text to the actual user agent, and in the XCode console it does show the new user agent value.
2013-12-16 19:03:42.200 App[736:60b] UA:Mozilla/5.0(iPhone;CPU iPhone OS 7_0_4,如 Mac OS X)AppleWebKit/537.51.1(KHTML,如 Gecko)Mobile/11B554a(iOS)
2013-12-16 19:03:42.200 App[736:60b] UA : Mozilla/5.0 (iPhone; CPU iPhone OS 7_0_4 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/11B554a(iOS)
但是当我运行我的应用程序时,它显示旧的.这是我在 UIWebView 中显示的网页中显示的用户代理的屏幕截图:
But when i run my App, it shows the old one. Here is a screenshot of the user agent shown in the web page i am showing in the UIWebView:
所以..我的问题是:我错过了什么?系统中是否存在不允许开发人员更改此数据的内容?
So.. my questions are: What am i missing?? is there something in the system that doesn´t allow developers to change this data??
谢谢.
推荐答案
我通过放入我的 AppDelegate.m 的 didFinishLaunchingWithOptions 来解决"我的问题以下代码:
I "solved" my problem by putting in the didFinishLaunchingWithOptions of my AppDelegate.m the following code:
NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:@"YOUR CUSTOM USER-AGENT", @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
问题是我不能将文本附加到用户代理本身,但我可以编写我需要的数据来区分从不同应用程序或网络浏览器对网页的访问.
The thing is that i cannot append the text to the user agent itself, but i can write the data i need to differentiate between the access to the web page from the different apps or web browsers.
感谢大家的帮助.
这篇关于用户代理在 NSMutableURLRequest 中没有改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!