【问题标题】:How to display https image from HTML content in UITextView?如何在 UITextView 中显示来自 HTML 内容的 https 图像?
【发布时间】: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 到浏览器的链接工作正常,但证书不是受信任的。我可以允许不受信任的证书显示图像吗?
  • 我只知道如何为网络视图执行此操作...但我认为这是您的问题。通常,浏览器会询问您是否要信任证书并且您允许。应用程序本身需要做同样的事情,但我不会自动发生。

标签: ios swift


【解决方案1】:

简而言之:您需要信任或拥有有效的证书

作为一种解决方法,以下方法可能会有所帮助

请勿将此代码用于生产。如果存在身份验证挑战,它将只信任任何证书。

在使用 html 之前,请尝试提出请求并信任证书。

你需要像导入SessionDelegate

YourController: UIViewController, URLSessionDelegate

然后实施身份验证挑战

func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
    print("auth challange")
    let trust = challenge.protectionSpace.serverTrust!
    let credential = URLCredential(trust: trust)
    completionHandler(.useCredential, credential)
}

然后致电您的网站

let url : NSString = "https://yourwebite.com"

let urlStr : NSString = url.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)! as NSString
let searchURL : URL = URL(string: urlStr as String)!

let request = NSMutableURLRequest(url: searchURL, cachePolicy: NSMutableURLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 300)

let session = URLSession(configuration: URLSession.shared.configuration, delegate: self, delegateQueue: OperationQueue.main)

let task = session.dataTask(with: request as URLRequest, completionHandler: {data, response, error -> Void in

})

task.resume()

应该有一个身份验证挑战,它会自动信任它。也许它也会被信任以进行进一步的调用,并且您将从 https 获取图像。

但正如所说,这可能只是一种解决方法。您需要一个受信任的证书。

【讨论】:

  • 不工作,(现在不能拥有受信任的证书,可能晚了)我有错误:- TIC SSL 信任错误- NSURLSession/NSURLConnection HTTP 加载失败(kCFStreamErrorDomainSSL,-9813)- HTTP 加载失败(错误代码:-1202 [3:-9813])
  • print("auth challenge") 会来吗? NSAllowsArbitraryLoads 也设置为 true 了吗?
  • “身份验证挑战”在日志中找到。并且 NSAllowsArbitraryLoads 始终设置为 true。
  • 这绝对是问题所在,然后为什么您没有在 html 中获取图像。身份验证挑战即将到来。您需要信任或拥有有效的证书才能使其正常工作。所以这将是您问题的解决方案。
猜你喜欢
  • 2020-04-09
  • 1970-01-01
  • 1970-01-01
  • 2019-12-10
  • 1970-01-01
  • 1970-01-01
  • 2021-02-23
  • 2011-11-22
  • 2023-03-25
相关资源
最近更新 更多