cordova使用笔记_在本地项目中添加cordova

创建一个cordova项目:http://www.jianshu.com/p/8773ab98a833
config.xml文件分析:http://www.jianshu.com/p/518e55452821

两种方式来使本地项目支持cordova

  • carthage:4.4.0以上版本支持
  • 手动配置

使用carthage来配置

git "git://git.apache.org/cordova-ios.git" "" # Apache

然而:

A shell task (/usr/bin/env git clone --bare --quiet git://git.apache.org/cordova-ios.git /Users/dawnwang/Library/Caches/org.carthage.CarthageKit/dependencies/cordova-ios) failed with exit code 128:
fatal: remote error: access denied or repository not exported: /cordova-ios.git

感受一下我内心的羊驼。。。。。。

手动加入

  1. 如果Xcode在运行,则先退出(官方这么说,虽然我不知道为啥)
  2. 拷贝config.xml和CordovaLib/CordovaLib.xcodeproj文件进入工程
  3. 打开Xcode,右键点击添加文件,将上面两个文件加入工程
  4. 在other linker flags中配置-force_load 和 -ObjC
  5. Build Phases->Link Binaries with Libraries.添加
AssetsLibrary.framework
CoreLocation.framework
CoreGraphics.framework
MobileCoreServices.framework
libCordova.a
  1. Target Dependencies中添加CordovaLib
  2. Header Search Paths
"$(TARGET_BUILD_DIR)/usr/local/lib/include"
"$(OBJROOT)/UninstalledProducts/include"
"$(OBJROOT)/UninstalledProducts/$(PLATFORM_NAME)/include"
"$(BUILT_PRODUCTS_DIR)"

运行报错

ld: file not found: -ObjC
clang: error: linker command failed with exit code 1 (use -v to see invocation)

解决:我的第4步的other linker flags是配置在了本地工程的target下,事实上应该放在CordovaLib.xcodeproj工程下
此时Command+R运行正常

使用CDVViewController

  • 首先我们要没有任何错误的将代码跑起来,然而我遇到了很多问题。
    • 通过上一步添加cordova,官方文档说可以正常使用CDVViewController了。
      我新创建一个VC继承了CDVViewController,然而却报错了。
    ‘Cordova/Cordova.h’file not found
    
    • 我傻啦吧唧得去找为什么自动生成的代码告诉我这个文件找不到。
      网上的说法很多,Header Search Path什么的,然而都没什么用。
      Cordova库里面事实上压根就没有Cordova.h这个文件,无论怎么操作都是不行的。所以把Cordova/Cordova.h换成
    #import "Cordova/CDV.h"
    

    完美解决。这算是Cordova的一个bug么!!很坑啊!!

    • PS:此处发现警告一枚,CDVCommandDelegate.h里面有一个block没有给参数类型。因为是Cordova的,遂忽略之。在Other Warning Flags中添加-Wno-strict-prototypes,再次编译,无警告。

    • 再次编译,结果在

    id backupWebStorage = [self.settings cordovaSettingForKey:@"BackupWebStorage"];
    

    崩溃了。

    • 查找原因是这个自定settings不能响应方法cordovaSettingForKey。由于这个方法是通过给字典增加分类的方式来添加的。所以需要在自己的工程的other linker flags中添加-ObjC。
    • 于是我们终于可以愉快的运行了
    • 由于之前没有添加www/这个文件夹,而且也没有重新配置content,所以程序默认去找www/index.html。会报错ERROR: Start Page at 'www/index.html' was not found.
      将www/文件夹添加进去就好了,要注意的是,添加的时候一定要选择Create folder references,文件夹是蓝色才可以。
  • 正确加载www/index.html后

    • 只有默认安装的几个插件可以使用
      在本地的工程中不能直接使用cordova plugin add 因为Current working directory is not a Cordova-based project.
      如何增加新的插件??
      1. 在原来的hellocordova的工程中使用cordova plugin add添加需要添加的插件
      2. 将www/文件直接复制到本地工程的对应目录下,主要原因在于要使用cordova_plugins.js和plugins/
        • cordova_plugins.js文件记录了所有的插件的信息
        • plugins/文件夹下则保存了对应插件的Js文件
        • 在finder中直接替换,所有的文件就自动加载到工程中了,我想这也是为什么之前添加www/这个文件要使用Create folder refrence这个选项的原因
      3. 将CordovaLib/Classes/Pievate/plugins/目录下对应的插件的原生代码添加到本地工程Cordovalib.xcodeproj/Private/Plugins下
      4. 配置对应的config.xml文件,如:
      
          
      
      
      1. 配置好index.html文件
      2. Command+R 运行正常
    • 如何增加自定义插件
      1. 创建一个继承于CDVPlugin的类:CustomPluginTest
      2. 给这个类一个方法
      - (void)customSelName:(CDVInvokedUrlCommand *)command {
      NSLog(@"%s______%@",__func__,command);}
      
      1. config.xml中配置
      
       
       
      
      1. 在index.html中写
      function customTest() {
       Cordova.exec(successFunction, failFunction, "customPluginTest", "customSelName", ["paramas"]);}
       function successFunction(){
         alert("successFunction");
       }
       function failFunction(){
         alert("failFunction");
       }
       

      1. Command+R运行正常,点击按钮customButton,正常打印了customSelName方法中的command。
      -[CustomPluginTest customSelName:]______
      

demo传送门:https://github.com/DawnWdf/DW_TestCordovaManully

后续:http://www.jianshu.com/p/43895cc97a01

参考:http://www.jianshu.com/p/e982b9a85ae8

你可能感兴趣的:(cordova使用笔记_在本地项目中添加cordova)