WKWebView仅显示网站的移动版本。

huangapple go评论176阅读模式
英文:

WKWEBVIEW to display only mobile version of website

问题

iPad版本的我的应用通过WKWebView显示网站的桌面版本。我想强制WKWebView仅显示不同网站的移动版本。有任何想法?

英文:

the iPad version of my app displays the desktop version of websites through a WKWEBVIEW. I would like to force the WKWEBVIEW to only show the mobile version of the different websites.

Any idea?

答案1

得分: 1

请问您是否已经尝试清除所有缓存并设置用户代理?

类似以下的方式:

let dataTypes = NSSet(array: [
    WKWebsiteDataTypeDiskCache,
    WKWebsiteDataTypeOfflineWebApplicationCache,
    WKWebsiteDataTypeMemoryCache,
    WKWebsiteDataTypeLocalStorage,
    WKWebsiteDataTypeCookies,
    WKWebsiteDataTypeSessionStorage,
    WKWebsiteDataTypeIndexedDBDatabases,
    WKWebsiteDataTypeWebSQLDatabases])
let date = NSDate(timeIntervalSince1970: 0)
WKWebsiteDataStore.defaultDataStore().removeData(ofTypes: dataTypes as! Set<String>, modifiedSince: date, completionHandler: { })

之后,在清理完成后,设置用户代理:

webview.customUserAgent = "Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5"

也许您可以尝试这个方法,但可能不是最佳解决方案。

英文:

Do you already tried to clear all caches and set user agent?

Something like this:

let dataTypes = NSSet(array: [
    WKWebsiteDataTypeDiskCache,
    WKWebsiteDataTypeOfflineWebApplicationCache,
    WKWebsiteDataTypeMemoryCache,
    WKWebsiteDataTypeLocalStorage,
    WKWebsiteDataTypeCookies,
    WKWebsiteDataTypeSessionStorage,
    WKWebsiteDataTypeIndexedDBDatabases,
    WKWebsiteDataTypeWebSQLDatabases])
let date = NSDate(timeIntervalSince1970: 0)
WKWebsiteDataStore.defaultDataStore().removeDataOfTypes(websiteDataTypes as! Set&lt;String&gt;, modifiedSince: date, completionHandler:{ })

and after this cleaning, set a user agent

webview.customUserAgent = &quot;Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5&quot;

Maybe you should try this one, but maybe isn't the best solution

huangapple
  • 本文由 发表于 2020年1月7日 00:59:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616100.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定