我假设您想要的是在某个地方有一些描述或文本,并允许在文本的某些部分内进行点击/点击,您想要使用 WKWebView。
我在很久以前做的一些应用程序中也使用WKWebView解决了这个问题,当然有几个解决方案,它只是其中一个,没有别的。
正如某些人对您所说,您可以使用loadHTMLString 函数以 JSON 格式或您想要的任何方式从您的服务器加载 HTML 字符串,但非常重要的是 HTML 格式正确且没有错误。
关于loadHTMLString 函数的一个非常重要的一点是关于您的评论...您通常会期望当您按下链接时会出现灰色背景,如下图所示,如果 HTML 没有某种 CSS 样式,则不会发生这种情况,因为如果没有,它具有任何 <a><\a> 的默认样式。
那么让我们看看下面的代码:
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var wkWebView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// The part of the HTMl you want to show
let description = "Let's start to visit the <a href=\"http://www.apple.com/\">Apple</a> website first and then if you want the <a href=\"http://www.w3schools.com\">Developer Apple</a> website."
// The default HTML with body and styles formatted and the description inside using string interpolation.
let htmlString = "<html><head><style type=\"text/css\">body {font-family: \"Lato-Regular\";font-size: 35px;color: #333333;margin: 0;}a {text-decoration: none; color: #999;}</style></head><body>\(description)</body></html>"
let preferences = WKPreferences()
preferences.javaScriptEnabled = false
// Configuration for any preferences you want to set
let configuration = WKWebViewConfiguration()
configuration.preferences = preferences
wkWebView = WKWebView(frame: CGRectMake(5, 35, self.view.bounds.width, self.view.bounds.height), configuration: configuration)
if let theWebView = wkWebView {
theWebView.loadHTMLString(htmlString, baseURL: NSURL())
theWebView.navigationDelegate = self
self.view.addSubview(theWebView)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func webView(webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
}
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}
func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) {
let alert = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Ok", style: .Default) { (UIAlertAction) -> Void in
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
})
presentViewController(alert, animated: true, completion: nil)
}
}
在上面的代码中,我设置了变量description,其中包含一些带有可点击元素的文本,并添加了两个委托方法didStartProvisionalNavigation 和didFinishNavigation 在状态栏中显示networkActivityIndicatorVisible 以显示一些进度关于点击可点击文本时页面的加载。很高兴注意到在htmlString 变量中我设置了一些样式来显示<a> 的一些颜色和字体,这取决于你。
我还添加了didFailProvisionalNavigation 函数以在某些情况下显示警报,URL 无效,例如添加的新HTTPTransportSecurityLayer 是iOS 9 仅允许https,在某些情况下您可以使用它来验证 url 并了解错误原因。
结果是普通UIViewController中的以下文本:
当你点击任何灰色文本时,url 将被加载到同一个 WKWebView 中,当然你可以个性化它以在你做的内部浏览器或其他东西中打开,这取决于你。
然后点击Apple即可立即看到结果:
如果您改为点击 Apple 开发者,您可以看到警报在起作用,因为 http 协议在 iOS 9 中的 HTTPTransportSecurityLayer 中给出错误:
如果您需要,我已准备好将项目上传到 Github。希望对您有所帮助。