一、新建测试项目
新建Cordova项目并添加ios平台
cordova create testCoco com.jyy.testCoco testCoco
cd testCoco
cordova platform add ios
二、自定义插件
1、生成插件
npm install -g plugman
plugman create --name [插件名字] --plugin_id [插件id] --plug_version [插件版本号]
直接cordova plugin add 会报错,需要package.json文件
cd [插件目录]
plugman createpackagejson [插件路径]
可能会报权限不足,加一个sudo就可以了,插件路径可以直接把插件文件夹拖进终端窗口,就可以自动填充插件路径
sudo plugman createpackagejson [插件路径]
然后一直回车,就可以创建完成,之后再继续下面的步骤
2、添加插件
cordova plugin add /Users/ecidi/Desktop/jyyWorkSpace/jyyLearnPack/SuperMap
解决办法:
由于官方https://github.com/CocoaPods/Specs.git下载太慢,使用清华镜像,https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git
https://mirrors.tuna.tsinghua.edu.cn/help/CocoaPods/参考镜像源给出的方案,执行以下命令
报以下错误
参考网上解决方案,设置git config --global http.postBuffer 1048576000,之后再进行git clone,报下面的错误
改用手机热点就可以下载成功,有条件可以开启网络代理工具
总结: 镜像、热点 或者 网络代理工具
三、测试插件
打开项目,编译失败
① 找不到第三方库头文件
解决:
在插件plugin.xml中增加 framework 标签将库引入,如果是pod引入的,需要新增一条pod内容,然后重新生成插件
<framework src="src/ios/frameworks/SuperMap.framework" custom="true"/>
<podspec>
<config>
<source url="https://github.com/brightcove/BrightcoveSpecs.git" />
<source url="https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git" />
</config>
<pods>
<pod name="RATreeView" spec="~> 2.1.2" />
<pod name="Masonry" />
</pods>
</podspec>
② 找不到pod库, Library not found for -l*
解决:
临时方案: 这个问题要么是多了要么是少了,查看 Library Search Paths ,多删少补
根本解决: 我发现重新建项目引入插件,之后不打开xcodeproj文件,而是直接打开xcworkspace文件再进行编译就不会报这个错,如果不想重新建项目,可以试试把 Developer/Xcode/DerivedData 文件夹下关于这个项目的内容删除掉,用xcode重新打开
③ for in循环报错,如下图
解决:
临时方案: 将 for( : ) 改为 for( in )
根本解决: 因为是语言问题,我灵机一动在xcode -> build Settings 里搜索了下 language,然后修改 Compile Sources As 为 Objective-C++ ,就不会再编译报错了。
④ libc abi.dylib: __cxa_guard_acquire detected deadlock
解决:
临时方案: 我这里使用了超图框架,重新导入SuperMap.bundle即可
根本解决: 之前在plugin.xml中使用的是framework标签,每次都需要重新导入SuperMap.bundle,经过一番百度之后,改用了resource-file标签,解决问题
<framework src="src/ios/frameworks/SuperMap.bundle" />
<resource-file src="src/ios/frameworks/SuperMap.bundle" />
⑤ Control may reach end of non-void function
解决:
临时方案: 这里报错的是RATreeView 一个 switch case循环,增加default返回即可
根本解决:
1、这里我看了下Github上的issues,的确是有人提这个问题的,解决方案是本地修改一下或者引用别人github上修改好的代码
2、我这里只是在plugin.xml 中 pods标签加了inhibit-all-warnings="true",就神奇的不报错了,inhibit-all-warnings禁止显示警告,这个要慎用,这里用的第三方库,所以影响不是很大,但不嫌麻烦的话最好还是使用方案1
<podspec>
<config>
<!-- 这里就不写了 -->
</config>
<pods inhibit-all-warnings="true"> <!-- 增加此配置 -->
<pod name="RATreeView" spec="~> 2.1.2" />
</pods>
</podspec>
⑥ undefined symbol: _utf8_nextCharSafeBody
解决:
临时方案: 在 Build Phases -> Link Binary With Libraries 中添加libicucore.tdb
根本解决: 在plugin.xml中引入此依赖
<framework src="libicucore.tbd">
⑦ .a 库报未找到
解决:
临时方案: 在 Build Settings-> Search Paths -> Library Search Paths 中添加 $(SRCROOT) recursive
根本解决: 查了下网上大家都是怎么引入.a库的,然后试了下修改plugin.xml 的内容,就解决了,随后查看了下Library Search Paths中,的确是比之前多了 .a库所在的文件夹
原先:
<framework src="src/ios/frameworks/libAsHttpRequest.a" custom="true">
修改为:
<source-file src="src/ios/frameworks/libAsHttpRequest.a" framework="true">