使用NSAttributedString解码html数据时遇到奇怪的崩溃

时间:2017-02-15 11:12:29

标签: swift

使用NSAttributedString解码html数据时出现奇怪的崩溃,请查看下面的函数和崩溃日志。在尝试解码奇怪的崩溃时

    func decodeHTML() -> String {
        var attributedString = NSAttributedString(string :self)
        let encodedData = self.data(using: String.Encoding.utf8)!
        let attributedOptions : [String: Any] = [
            NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
            NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue
        ]
        //webkitlegacy crash. It should run in main thread

        if Thread.isMainThread {
            do {
                attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)
            } catch {
                print("Unexpected error occured inside decodeHTML")
            }
        } else {
            DispatchQueue.main.sync {
                do {
                    attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)
                } catch {
                    print("Unexpected error occured inside decodeHTML")
                }
            }
        }
        return attributedString.string
    }
}    

I got below crash logs , please let me know how to avoid such crash

获取以下日志:

  

0 libobjc.A.dylib 0x18b332f30 objc_msgSend + 16       1 WebKitLegacy 0x19242a25c - [_ WebSafeForwarder forwardInvocation:] + 132       2 CoreFoundation 0x18c8ea078 转发 + 404       3 CoreFoundation 0x18c7e459c _CF_forwarding_prep_0 + 92       4 CoreFoundation 0x18c8ec160 调用_ + 144       5 CoreFoundation 0x18c7dfc3c - [NSInvocation invoke] + 284       6 WebCore 0x19139ac78 HandleDelegateSource(void *)+ 108       7 CoreFoundation 0x18c894278 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 24       8 CoreFoundation 0x18c893bc0 __CFRunLoopDoSources0 + 524       9 CoreFoundation 0x18c8917c0 __CFRunLoopRun + 804       10 CoreFoundation 0x18c7c0048 CFRunLoopRunSpecific + 444       11 UIFoundation 0x1926e919c - [NSHTMLReader _loadUsingWebKit] + 1860       12 UIFoundation 0x1926ea424 - [NSHTMLReader attributionString] + 28       13 UIFoundation 0x192689cb4 _NSReadAttributedStringFromURLOrData + 4688       14 UIFoundation 0x1926889bc - [

1 个答案:

答案 0 :(得分:0)

试试这个 -

extension String {
 init(htmlEncodedString: String) {
    self.init()
    guard let encodedData = htmlEncodedString.data(using: .utf8) else {
        self = htmlEncodedString
        return
    }

    let attributedOptions: [String : Any] = [
        NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
        NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue
    ]

    do {
        let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)
        self = attributedString.string
    } catch {
        print("Error: \(error)")
        self = htmlEncodedString
    }
  }
}

调用以上方法 -

 let html = "<center>Here is some <b>HTML</b></center>"
    let decodedString = String(htmlEncodedString: html)
    print(decodedString)