uniapp ios原生插件开发 (framework,cocopods)
<p>原文地址:https://zhanglei.blog.csdn.net/article/details/123221947</p><h2 id="一了解uniapp-插件的开发方式">一、了解UniApp 插件的开发方式</h2>
<ul>
<li>Xcode Framework 、 Static Library</li>
<li>基于Cocoapods开发 pod lib</li>
</ul>
<p>已知UniApp的插件开发方式有两种, 第一种 Xcode Framework的方式,这种方式是官方指定的方式。 这种方式的优点就是简单直接, 但也有不足,比如当插件需要引入一些三方库时,操作起来就不是那么方便。 而使用Cocoapods则可以很方便的引入三方库。 接下来,本文探索使用Cocoapods的方式来进行UniApp插件开发。</p>
<p>探索之前可以先了解一下官方插件开发的流程及步骤:<br>
iOS插件开发教程</p>
<h2 id="二准备">二、准备</h2>
<h3 id="1安装cocoapods">1.安装Cocoapods</h3>
<p>如果是首次使用Cocoapods ,则需要先安装cocoapods。具体的安装步骤非本文重点,可参考如下文章:<br>
安装Cocoapods的步骤</p>
<h3 id="2-下载uniapp-ios-sdk">2. 下载UniApp iOS SDK</h3>
<p>下载地址</p>
<h2 id="三将hbuilder-uniplugindemo转成cocoapods">三、将HBuilder-UniPluginDemo转成Cocoapods</h2>
<p>找到UniApp iOS SDK下载的目录,看一下官方提供的目录结构<br>
<img src="https://img-blog.csdnimg.cn/c434d590ce04407093c3ffb16852943b.png"></p>
<h3 id="podfile的创建及配置">podfile的创建及配置</h3>
<p>打开“命令行”工具,<code>cd</code> 到 <code>HBuilder-uniPlugin.xcodeprj</code> 工程所在目录下, 并执行</p>
<pre><code class="language-objectivec">pod init
</code></pre>
<p>来创建Podfile 文件模板<br>
<img src="https://img-blog.csdnimg.cn/8ba13ccdec2a4a91bff73263eff9dbab.png"><br>
此时目录下会多出一个 ‘<code>Podfile</code>’ 的文件<br>
<img src="https://img-blog.csdnimg.cn/633d286c910448a6a36ea7b6b64c1fe8.png"><br>
打开<code>Podfile</code> 文件, 配置相关设置如下 :</p>
<pre><code class="language-objectivec"># Uncomment the next line to define a global platform for your project
platform :ios, '10.0'
#忽略pod所有库警告
inhibit_all_warnings!
workspace 'uniPlugins'
#关闭所有pod库的BITCODE
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['ENABLE_BITCODE'] = 'NO'
end
end
end
target 'HBuilder' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
end
</code></pre>
<p><strong>配置忽略Pod所有库的警告</strong></p>
<pre><code class="language-objectivec">inhibit_all_warnings!
</code></pre>
<p><strong>配置workspace名称</strong></p>
<pre><code class="language-objectivec">workspace 'uniPlugins'
</code></pre>
<p><strong>关闭所有pod库的BITCODE</strong></p>
<pre><code class="language-objectivec">post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['ENABLE_BITCODE'] = 'NO'
end
end
end
</code></pre>
<h3 id="使用pod-lib-创建插件工程">使用pod lib 创建插件工程</h3>
<p>在HBuilder-uniPluginDemo目录下创建一个自定义插件目录 ‘<code>custom-plugins</code>’ 用来存放自定义的插件工程,目录结构如下:<br>
<img src="https://img-blog.csdnimg.cn/f713c1c7cb854ea188ab6fd9709178e9.png"><br>
使用命令行工具, <code>cd</code> 到 <code>custom-plugins</code>目录下, 并执行</p>
<pre><code class="language-objectivec">pod lib create rz-testplugin
</code></pre>
<p><img src="https://img-blog.csdnimg.cn/be59603cdcc74e9e937e9c00c2f7efa4.png"><br>
按回车,执行命令。 会从github上加载创建pod工程的模板。 模板下载结束后,会出现如下引导:<br>
<img src="https://img-blog.csdnimg.cn/1187b1b0e7de4cc7b18b596d57cc7ef4.png"></p>
<blockquote>
<p>按上述引导完成配置,在完成创建后会自动打开pod 工程, 目前用不到此工程,接着关闭即可</p>
</blockquote>
<p>回到目录 HBuilder-uniPluginDemo下,找到<code>Podfile</code> 文件,并打开<br>
将本地新创建的pod工程配置进去</p>
<pre><code class="language-objectivec">target 'HBuilder' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
#重要: 导入自定义组件库
pod 'rz-testplugin', :path =>'./custom-plugins/rz-testplugin'
end
</code></pre>
<p><strong>完整的Podfile文件如下:</strong></p>
<pre><code class="language-objectivec"># Uncomment the next line to define a global platform for your project
platform :ios, '10.0'
#忽略pod所有库警告
inhibit_all_warnings!
workspace 'uniPlugins'
#关闭所有pod库的BITCODE
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['ENABLE_BITCODE'] = 'NO'
end
end
end
target 'HBuilder' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
#重要: 导入自定义组件库
pod 'rz-testplugin', :path =>'./custom-plugins/rz-testplugin'
end
</code></pre>
<p>Podfile配置完成后, 使用命令行工具在Podfile所在目录下,执行命令</p>
<pre><code class="language-objectivec">pod install
</code></pre>
<p>更多内容详情: https://zhanglei.blog.csdn.net/article/details/123221947</p>
</div>
<div id="MySignature" role="contentinfo">
<p>本文来自博客园,作者:reyzhang,转载请注明原文链接:https://www.cnblogs.com/reyzhang/p/17094730.html</p><br><br>
来源:https://www.cnblogs.com/reyzhang/p/17094730.html
頁:
[1]