【发布时间】:2018-01-30 01:26:04
【问题描述】:
我当前使用以下代码在 TextView 中加载 html 内容(来自 JSON WS):
if let stringHTML = contentHTML.data(using: String.Encoding.unicode, allowLossyConversion: true) {
do {
let attrStr = try NSMutableAttributedString(
data: stringHTML,
options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
attrStr.enumerateAttribute(NSAttachmentAttributeName, in: NSMakeRange(0, attrStr.length), options: .init(rawValue: 0), using: { (value, range, stop) in
if let attachement = value as? NSTextAttachment {
let image = attachement.image(forBounds: attachement.bounds, textContainer: NSTextContainer(), characterIndex: range.location)!
let screenSize: CGRect = UIScreen.main.bounds
if image.size.width > screenSize.width-20 {
let newImage = image.resizeImage(scale: (screenSize.width-2)/image.size.width)
let newAttribut = NSTextAttachment()
newAttribut.image = newImage
attrStr.addAttribute(NSAttachmentAttributeName, value: newAttribut, range: range)
}
}
})
contentTextView.attributedText = attrStr
} catch _ {
contentTextView.text = ""
}
}
这适用于 http 图像,但不适用于 https。 我在 info.plist 中将 NSAllowsArbitraryLoads 设置为 true :
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
但不显示 https 图像。 https 图像可以显示在浏览器中,但证书不受信任。如何允许不受信任的 https 证书? 有什么想法吗?
【问题讨论】:
-
你的图片可以通过 https 获得吗?
-
是的,html 内容中的图像格式如下:HTTPS://myserver/api/filestorages/download/…>(HTTPS 是大写的,因为如果不是,则在 stackoverflow cmets 中不可见)
-
我的意思是,您可以将链接从 html 复制到浏览器中(缓存已清除)并看到您的图像吗?证书是可信的吗?
-
从 html 到浏览器的链接工作正常,但证书不是受信任的。我可以允许不受信任的证书显示图像吗?
-
我只知道如何为网络视图执行此操作...但我认为这是您的问题。通常,浏览器会询问您是否要信任证书并且您允许。应用程序本身需要做同样的事情,但我不会自动发生。