UIWebView not loading mobile version of site with loadRequest

谁说我不能喝 提交于 2019-12-12 21:14:10

问题


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).

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.

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.


回答1:


I found the solution to my problem today.

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.

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: I found this cookie solution in another stackoverflow question about cookies.




回答2:


If the website does not have a full mobile version, there's NOTHING you can do. :(



来源:https://stackoverflow.com/questions/8542038/uiwebview-not-loading-mobile-version-of-site-with-loadrequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!