Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

Swift iOS how to get desktop version URL response

I want to get the desktop version URL response in iOS devices. But I always get the mobile version URL response. Here is my code:

var request = URLRequest(url: URL(string: "https://www.youtube.com")!)
request.addValue("Chrome Safari", forHTTPHeaderField: "User-Agent")
let session = URLSession.init(configuration: URLSessionConfiguration.default)
let task = session.dataTask(with: request) { (data, response, error) in
    if let data = data, let str = String(data: data, encoding: .utf8) {
        print(str)
    }
}
task.resume()

That code will get mobile version response. How can I get desktop version URL response?

I want to get the og information of the desktop response, not using WKWebView to present website.

question from:https://stackoverflow.com/questions/65880616/swift-ios-how-to-get-desktop-version-url-response

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Possible duplicate:

How can I request a desktop version of a webpage using UIWebView in Swift 3.0?

Answer:

You could use WKWebView customUserAgent property to get a desktop version of web-page:

https://developer.apple.com/documentation/webkit/wkwebview/1414950-customuseragent

webview.customUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36"

Of course, you need to modify the web-browsers version to get an actual response:

Mozilla Firefox:
https://www.mozilla.org/en-US/firefox/releases/

Google Chrome:
https://www.whatismybrowser.com/guides/the-latest-version/chrome

Apple Safari:
https://developer.apple.com/documentation/safari-release-notes

https://www.whatismybrowser.com/guides/the-latest-version/safari


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...