如何与主线程swift文件交谈以访问主线程变量

时间:2022-02-20 21:03:26

I am a very experienced Android dev but very new to iOS dev using swift. Right now I have setup a UIWebview and a NSURLProtocol using canInitWithRequest (2 separate swift files). The canInitWithRequest method works great but I need to acccess the UIWebview object in order to call functions on the UIWebview object.

我是一个非常有经验的Android开发人员,但对于使用swift的iOS dev来说是非常新的。现在我使用canInitWithRequest(2个单独的swift文件)设置了UIWebview和NSURLProtocol。 canInitWithRequest方法工作得很好但我需要访问UIWebview对象才能调用UIWebview对象上的函数。

For example. if request == google.com then UIWebview.something().

例如。如果请求== google.com然后是UIWebview.something()。

In Android in order to talk with the Main activity thread I just simply create a interface, implement it in my main thread and now I have access to everything on that main activity class.

在Android中,为了与Main活动线程交谈,我只需创建一个接口,在我的主线程中实现它,现在我可以访问该主活动类的所有内容。

Thanks for any help.

谢谢你的帮助。

1 个解决方案

#1


0  

You may be looking for the method -webView(_:shouldStartLoadWithRequest:navigationType:) in the UIWebViewDelegate protocol.

您可能正在UIWebViewDelegate协议中查找方法-webView(_:shouldStartLoadWithRequest:navigationType :)。

By implementing this interface, you can have a different object (e.g. the object in your "separate Swift file") determine whether or not to load an NSURLRequest and even do run custom behavior first:

通过实现此接口,您可以拥有一个不同的对象(例如,“单独的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
    }
}

#1


0  

You may be looking for the method -webView(_:shouldStartLoadWithRequest:navigationType:) in the UIWebViewDelegate protocol.

您可能正在UIWebViewDelegate协议中查找方法-webView(_:shouldStartLoadWithRequest:navigationType :)。

By implementing this interface, you can have a different object (e.g. the object in your "separate Swift file") determine whether or not to load an NSURLRequest and even do run custom behavior first:

通过实现此接口,您可以拥有一个不同的对象(例如,“单独的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
    }
}