capacitor icon indicating copy to clipboard operation
capacitor copied to clipboard

feat: Can not react to WKNavigationDelegate in Plugin

Open ghost opened this issue 4 years ago • 0 comments

This issue is related to developing ios plugins.

Issue description: CAPBridgeViewController is WKNavigationDelegate so no other plugins can be a delegate for navigation of the webview.

Reacting to important WKWebview navigation events like:

  • didFinish
  • didStartProvisionalNavigation
  • didFailProvisionalNavigation ...

Is now impossible in a plugin since CAPBridgeViewController is already the WKNavigationDelegate.

I wanted to write a plugin like this (I actually had 2 finished):

@objc(SomePlugin)
public class SomePlugin: CAPPlugin, WKNavigationDelegate {
    
    public override func load() {
        self.bridge.getWebView()?.navigationDelegate = self
    }
    
    public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        // Do some logic
    }

   // Other WKNavigationDelegate protocol methods
}

Proposed solutions: Solution 1: Implement all functions of the WKNavigationDelegate and let them send a notifcation we can subscribe to in our plugins.

Refering to line 220 in CAPBridgeViewController:

public func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
    // Reset the bridge on each navigation
    bridge!.reset()
   NotificationCenter.default.post(name: .didStartProvisionalNavigation, object: nil)
  }
```

**Solution 2:**
Implement multiple custom delegation pattern in CAPBridgeViewController

**Solution 3:**
Allow us to use callbacks
[https://medium.cobeisfresh.com/why-you-shouldn-t-use-delegates-in-swift-7ef808a7f16b](https://medium.cobeisfresh.com/why-you-shouldn-t-use-delegates-in-swift-7ef808a7f16b)


I am happy to start a PullRequest with any of these solutions. We need this. We use multiple apps and they are hosted remotely.

ghost avatar Jan 20 '20 13:01 ghost