0

我正在制作一个简单的UIWebViewiOS 应用程序。在其中,当我希望它显示为完整版时,我要显示的网站显示为移动版。

我有这个代码viewDidLoad:

NSString *fullURL = @"www.facebook.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// [request setValue:@"Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3" forHTTPHeaderField:@"User-Agent"];
[self.webView loadRequest: request];

我找到的注释掉的代码使网站显示为移动版本。有谁知道我如何将注释行更改为桌面版本?当注释行保持注释时,它仍然作为移动版本运行。

谢谢,那真的很有帮助!

4

1 回答 1

1

为 HTTP 请求设置桌面用户代理:例如 '@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2";'

但是这对 webview 不起作用,因为 webview 不使用为请求设置的代理。相反,它使用来自 NSUserDefaults 的代理。

所以:

NSString *secretagent = @"%%MyUserAgent%%"; //the one you chose
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:secretAgent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

要使其生效,请在加载 UIWebview 之前执行此操作!

于 2013-06-18T08:03:18.920 回答