Codava

2020-04-23  本文已影响0人  fanren

一、安装Codava

二、项目集成Codava

#ifndef MySwiftProject_Bridging_Header_h
#define MySwiftProject_Bridging_Header_h

#import <Cordova/CDVViewController.h>
#import <Cordova/CDVCommandDelegateImpl.h>
#import <Cordova/CDVCommandQueue.h>
#import <Cordova/CDVPlugin.h>

#endif /* MySwiftProject_Bridging_Header_h */
class ViewController: CDVViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

运行结果

三、js调用原生

@interface TestPlugin : CDVPlugin
- (void)test:(CDVInvokedUrlCommand *)command;
@end
  <feature name="TestPlugin">
      <param name="ios-package" value="TestPlugin" />
  </feature>
// 引入cordova
    <script type="text/javascript" src="cordova.js"></script>
  function getImage(){
//调用原生方法(TestPlugin类中的test方法,参数是"123")
      Cordova.exec(successFunction, failedFunction, "TestPlugin","test", ["123"]);
  }
  function successFunction() {
      alert("成功调用原生方法的回调");
  }
  function failedFunction() {
      alert("失败调用的回调");
  }

四、原生调用js方法

  function uploadData(str, code) {
      alert(str + ' , ' + code);
  }
class ViewController: CDVViewController {
    @objc func respondsToRightItem() {
        let js = "uploadData('图片', '123456')"
        self.commandDelegate.evalJs(js)
    }

   // 调用js方法,获取函数uploadData返回值(result)
    @objc func respondsToRightItem2() {
        self.webViewEngine.evaluateJavaScript("uploadData('aaa', 'bbb')") { (result, _) in
            print(result)
        }
    }
}

五、demo

demo

六、使用WKWebView

七、错误问题

Build Setting->Search Paths->Header Search Paths中加入
"$(OBJROOT)/UninstalledProducts/$(PLATFORM_NAME)/include"
Debug和Release中都要加入

上一篇下一篇

猜你喜欢

热点阅读