contentsof:url加载截断URL的url内容

问题描述 投票:1回答:1

当我使用contentsof:url时,它会在检索内容之前截断url,导致与WKWebView中显示的内容不同。

例如

contents = try String(contentsOf:https://www.amazon.com/gp/aw/d/B00BECJ4R8/ref=mp_s_a_1_1?ie=UTF8&qid=1531620716&sr=8-1-spons&pi=AC_SX236_SY340_QL65&keywords=cole+haan&psc=1

返回此页面的内容:https://www.amazon.com/gp/aw/d/B00BECJ4R8/

为什么会这样?是否有另一种方法可以让您读取实际URL的内容而不是截断的URL?

任何建议,如果非常感谢。

谢谢。

swift url wkwebview
1个回答
0
投票

你不应该使用String(contentsOf:)加载网站。您应该使用URL加载系统进行此工作,然后将该对象传递回webView.load(_:)中的viewDidLoad()方法

 let urlString = "https://www.amazon.com/dp/B00BECJ4R8/?tag=stackoverflow17-20"
 // URL construct may fail in case of the String not being a properly formatted URL, so unwrap it. 
 if let url = URL(string: urlString) { 
   // Create a URLRequest instance with the new url.
   let request = URLRequest(url: url) 
   // Load the request.  
   webView.load(request)
 }
© www.soinside.com 2019 - 2024. All rights reserved.