使UIWebview显示dektop而不是移动页面

时间:2013-06-18 07:57:47

标签: ios objective-c uiwebview http-headers nsmutableurlrequest

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

我在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];

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

谢谢你真的很有帮助!

1 个答案:

答案 0 :(得分:1)

为HTTP请求设置桌面用户代理:例如'@“Mozilla / 5.0(Macintosh; Intel Mac OS X 10_6_8)AppleWebKit / 537.13 +(KHTML,类似Gecko)版本/ 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之前执行此操作!