UIWebView not loading mobile version of site with

2019-07-13 09:58发布

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.

2条回答
再贱就再见
2楼-- · 2019-07-13 10:48

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

查看更多
Melony?
3楼-- · 2019-07-13 10:51

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.

查看更多
登录 后发表回答