[英]How to talk with main thread swift files to access main thread variables
我是一个非常有经验的Android开发人员,但是对使用Swift的iOS开发人员来说却是新手。 现在,我使用canInitWithRequest(2个独立的swift文件)设置了一个UIWebview和一个NSURLProtocol。 canInitWithRequest方法的效果很好,但是我需要访问UIWebview对象才能调用UIWebview对象上的函数。
例如。 如果请求== google.com,则为UIWebview.something()。
在Android中,为了与Main活动线程进行对话,我只是简单地创建一个接口,在我的主线程中实现它,现在我就可以访问该main活动类中的所有内容。
谢谢你的帮助。
您可能正在UIWebViewDelegate
协议中寻找方法-webView(_:shouldStartLoadWithRequest:navigationType:)
。
通过实现此接口,您可以让另一个对象(例如,“单独的Swift文件”中的对象)确定是否加载NSURLRequest
甚至首先运行自定义行为:
extension MyObject: UIWebViewDelegate {
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if request.URL == NSURL(string: "google.com") {
// Do your custom behavior...
webView.something()
}
// Allow the webView to load the request
return true
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.