apache / cordova-ios

Apache Cordova iOS
https://cordova.apache.org/
Apache License 2.0
2.15k stars 987 forks source link

Can't run native method from javascript #1248

Closed eskygit closed 1 year ago

eskygit commented 1 year ago

Hi, I have one problem. I am using CDVViewController for iOS. https://cordova.apache.org/docs/en/11.x/guide/platforms/ios/webview.html

I Can't run native method from javascript in WKWebview of CDVViewController property. I Want to set WKWebViewConfiguration.userContentController to WKWebview of CDVViewController, but I can't it. Please let me know if you have any solution.

breautek commented 1 year ago

In order for JavaScript to call on native methods, a plugin needs to be implemented to provide that API.

On the javascript side, cordova offers an exec function, which is generally implemented on the JS side of the plugin, abstracted by a JS function (to hide implementation details of the plugin, such as the internal service name, and actions etc. Cordova will make the exec call to the appropriate CordovaPlugin whose execute implementation will receive the action along with arguments. The CordovaPlugin can then take this information and map it to it's own native method by action and do whatever native work necessary.

Relevant resources:

I Want to set WKWebViewConfiguration.userContentController to WKWebview of CDVViewController

Unfortunately I can't provide specific pointers, I'm not sure how CordovaPlugin can gain access to the CDVViewController, but a more appropriate place to ask a support question like this is our Slack community.