Html Swift NSURLSession下载任务未检索图像资产
我正在从本地Jetty 9服务器下载一个类似这样的html页面Html Swift NSURLSession下载任务未检索图像资产,html,ios,swift,nsurlsession,nsurlsessiondownloadtask,Html,Ios,Swift,Nsurlsession,Nsurlsessiondownloadtask,我正在从本地Jetty 9服务器下载一个类似这样的html页面 <h1>Hello</h1> <img src="tiles/tile00.jpg"> <img src="tiles/tile01.jpg"> 页面以Chrome浏览器显示图像 但是,当我使用下面的Swift代码导航到该页面时,我只收到一个请求 / 文本呈现,但图像仅呈现为[?]图标 @IBAction func displayWebPage(sender: UIButton)
<h1>Hello</h1>
<img src="tiles/tile00.jpg">
<img src="tiles/tile01.jpg">
页面以Chrome浏览器显示图像
但是,当我使用下面的Swift代码导航到该页面时,我只收到一个请求
/
文本呈现,但图像仅呈现为[?]
图标
@IBAction func displayWebPage(sender: UIButton) {
let h2Url = NSURL(string: "https://localhost:8443")!
let downloadDelegate = Deleg(view: self)
let ses = NSURLSession(
configuration: NSURLSessionConfiguration.defaultSessionConfiguration(),
delegate: downloadDelegate,
delegateQueue: nil
)
ses.downloadTaskWithURL(myUrl).resume()
}
func completed(location: NSURL) {
print("downloaded")
var htmlString = "NON, MERCÍ"
do {
htmlString = try String.init(
contentsOfURL: location,
encoding: NSUTF8StringEncoding
)
} catch {
print("ERRORED OUT")
fatalError()
}
print("contents: \(htmlString)")
self.webView.loadHTMLString(htmlString, baseURL: nil)
}
class Deleg: NSObject, NSURLSessionDelegate, NSURLSessionDownloadDelegate {
var view: ViewController
init(view: ViewController) {
self.view = view
}
func URLSession(
session: NSURLSession,
downloadTask: NSURLSessionDownloadTask,
didFinishDownloadingToURL location: NSURL
) {
self.view.completed(location)
}
}
有没有办法说服NSURLSession下载它下载的html页面中链接到的资产
一种解决方案是让
UIWebView
下载链接的资产,但我将无法使用自签名证书。您可以尝试使用XML/HTML解析来提取这些链接,然后使用NSURLSession下载它们。您可以使用NSXMLParser,或者如果您熟悉XPath或CSS选择器,您可以尝试这个库:好的,谢谢,我会尝试的。我需要让NSURLSession重用TCP连接来模拟真实浏览器的功能。不过我认为这是可能的。您可以尝试使用XML/HTML解析来提取这些链接,然后使用NSURLSession下载它们。您可以使用NSXMLParser,或者如果您熟悉XPath或CSS选择器,您可以尝试这个库:好的,谢谢,我会尝试的。我需要让NSURLSession重用TCP连接来模拟真实浏览器的功能。但我认为这是可能的。
@IBAction func displayWebPage(sender: UIButton) {
let h2Url = NSURL(string: "https://localhost:8443")!
let downloadDelegate = Deleg(view: self)
let ses = NSURLSession(
configuration: NSURLSessionConfiguration.defaultSessionConfiguration(),
delegate: downloadDelegate,
delegateQueue: nil
)
ses.downloadTaskWithURL(myUrl).resume()
}
func completed(location: NSURL) {
print("downloaded")
var htmlString = "NON, MERCÍ"
do {
htmlString = try String.init(
contentsOfURL: location,
encoding: NSUTF8StringEncoding
)
} catch {
print("ERRORED OUT")
fatalError()
}
print("contents: \(htmlString)")
self.webView.loadHTMLString(htmlString, baseURL: nil)
}
class Deleg: NSObject, NSURLSessionDelegate, NSURLSessionDownloadDelegate {
var view: ViewController
init(view: ViewController) {
self.view = view
}
func URLSession(
session: NSURLSession,
downloadTask: NSURLSessionDownloadTask,
didFinishDownloadingToURL location: NSURL
) {
self.view.completed(location)
}
}