因为我们的App最低支持iOS8.0,但是测试也没有iOS8的机器,所以在做加载UILabel html 标签文本的时候我一直是异步加载,主线程刷新UI,在iOS 9-12一直没发现会有问题。因为加载的过程略慢,而且这样异步加载用户体验也比较好。
之前的做法:
DispatchQueue(label: "contentText").async {
do{
let htmString = "..." // 标签文本
if let strData = htmString.(using: String.Encoding.unicode, allowLossyConversion: true)
let attrStr = try? NSMutableAttributedString(data: strData, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil){
DispatchQueue.main.async {
...
}
}
}
}
项目上线后,发现iOS 8 系统中会有导致崩溃,所以在调查后,发现iOS 8 中异步加载会导致必崩。在查阅后发现官方文档有这么一句话:
The HTML importer should not be called from a background thread (that is, the options dictionary includes NSDocumentTypeDocumentAttribute with a value of NSHTMLTextDocumentType).
不用多说,修改后:
do{
let htmString = "..." // 标签文本
if let strData = htmString.(using: String.Encoding.unicode, allowLossyConversion: true),
let attrStr = try? NSMutableAttributedString(data: strData, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil){
...
}
}
再测试iOS 8就没有问题了。

网友评论