如何在UITextView中打开外部链接?点击链接报-10814错误求助
解决UITextView点击HTML链接出现OSStatus error -10814的问题
错误原因
OSStatus error -10814对应kLSApplicationNotFoundErr,说白了就是系统找不到能打开当前URL的应用。你碰到这个问题,基本是因为HTML里的链接用了applewebdata://协议——这是iOS解析本地HTML时生成的内部协议,UIApplication.shared.open(_:)根本处理不了它。
解决方案
1. 分协议处理链接
修改textView:shouldInteractWithURL:in:interaction:方法,先判断URL的协议类型,applewebdata协议的内容用WKWebView加载,其他正常链接再走UIApplication.open:
import WebKit func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool { if URL.scheme == "applewebdata" { // 用WKWebView展示applewebdata内容 let webView = WKWebView(frame: textView.bounds) webView.load(URLRequest(url: URL)) // 把webView加到当前页面,这里示例是铺满根控制器视图,你可以改成弹窗或者其他布局 if let rootVC = textView.window?.rootViewController { rootVC.view.addSubview(webView) webView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ webView.topAnchor.constraint(equalTo: rootVC.view.topAnchor), webView.leadingAnchor.constraint(equalTo: rootVC.view.leadingAnchor), webView.trailingAnchor.constraint(equalTo: rootVC.view.trailingAnchor), webView.bottomAnchor.constraint(equalTo: rootVC.view.bottomAnchor) ]) } } else if UIApplication.shared.canOpenURL(URL) { UIApplication.shared.open(URL) } else { print("当前设备无法打开该链接") } return false }
2. 清理无用的info.plist配置
applewebdata是iOS内部协议,完全不需要加到LSApplicationQueriesSchemes里,直接删掉这段配置:
<!-- 删除以下内容 --> <key>LSApplicationQueriesSchemes</key> <array> <string>applewebdata</string> </array>
额外排查点(如果是http/https链接出错)
要是你的链接是正常的http/https协议却报这个错,检查这两点:
- 确认URL格式正确,没有编码错误
- iOS 9之后http/https默认允许打开,不需要额外加
LSApplicationQueriesSchemes配置,除非你用了其他自定义协议
内容的提问来源于stack exchange,提问作者SwiftTry
相关产品推荐
相关产品推荐

