UIWebView无法使用loadRequest加载网站的移动版本 [英] UIWebView not loading mobile version of site with loadRequest

查看:106
本文介绍了UIWebView无法使用loadRequest加载网站的移动版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用UIWebView在我的应用程序中嵌入网站的移动版本.但是,当我执行以下操作时:

I'm trying to use a UIWebView to embed a mobile version of a site in my app. However, when I do the following:

NSURL *url = [NSURL URLWithString:webLink];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[self.webView loadRequest:request];

该网站的完整版本已加载.然后,当我单击此页面中的任何链接时,它将处理该网站的移动版本(从一开始我就希望它能够运行).

the full version of the site is loaded. Then when I click any of the links in this page, it will process the mobile version of the site (as I would like it to from the very beginning).

我已经尝试过与用户代理打交道,但是没有运气.我不认为它正在查看用户代理来确定是否加载页面的移动版本,因为我将Firefox用户代理设置为iPhone,并且仍继续加载完整版本.

I've tried messing around with the user-agent, with no luck. I don't think it's looking at the user-agent to determine whether or not to load the mobile version of the page, because I set my Firefox user-agent to iPhone and it still proceeded to load the full version.

如果有帮助,我尝试加载的站点是使用IP Board 3.1.4的留言板.我试着在网上寻找,看看它如何确定是否显示移动版本,没有运气.

If it helps any, the site I'm trying to load is a message board, using IP Board 3.1.4. I tried looking online to see how it determines whether or not to display mobile versions, with no luck.

推荐答案

我今天找到了解决问题的方法.

I found the solution to my problem today.

我在IP Board支持论坛上发布了我的问题,并得到回复说IP Board检查用户代理中的"iPhone"或"mobileApp" cookie.由于搞乱用户代理对我不起作用,因此我采用了后一种方法.

I posted my problem on the IP Board support forum, and got a response back saying that the IP Board checks for either "iPhone" in the user-agent, or a "mobileApp" cookie. Since messing with the user-agent wasn't working for me, I went with the latter approach.

我将此代码放入我的appdelegate中的applicationDidBecomeActive方法中:

I put this code into my applicationDidBecomeActive method in my appdelegate:

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];

NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:@"mobileApp" forKey:NSHTTPCookieName];
[cookieProperties setObject:@"1" forKey:NSHTTPCookieValue];
[cookieProperties setObject:@"www.example.com" forKey:NSHTTPCookieDomain];
[cookieProperties setObject:@"www.example.com" forKey:NSHTTPCookieOriginURL];
[cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
[cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];

NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];

PS:我在另一个关于cookie的stackoverflow问题中找到了这个cookie解决方案.

PS: I found this cookie solution in another stackoverflow question about cookies.

这篇关于UIWebView无法使用loadRequest加载网站的移动版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆