iOS 私有库搭建

532 阅读3分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第2天,点击查看活动详情

新建项目 - 远端

远端新建代码仓库版本管理仓库

image.png

新建项目 - 创建本地项目

1、 新建存放私有库的文件夹,例如:XLTest。

2、 打开终端cd到该文件夹下,执行以下命令,输入相关信息,成功会自动打开项目。

pod lib create XLBaseTest

注:XLBaseTest远端仓库名称保持一致

M1电脑抛出异常/Library/Ruby/Gems/2.6.0/gems/ffi-1.15.5/lib/ffi/library.rb:275: [BUG] Bus Error at 0x00000001009dc000

解决:安装ffi,执行 sudo arch -x86_64 gem install ffi后,终端cd到Example下重新pod install,若还是失败执行 arch -x86_64 pod install

相关信息

What platform do you want to use? [ iOS / macOS ]

 > iOS

What language do you want to use? [ Swift / ObjC ]

 > ObjC

Would you like to include a demo application with your library? [ Yes / No ]

 > Yes

Which testing frameworks will you use? [ Specta / Kiwi / None ]

 > None

Would you like to do view based testing? [ Yes / No ]

 > No

What is your class prefix?

 > XL

目录结构

-- XLBaseTest.podspec

    |-- Example(使用样例)
    |   |-- Podfile
    |   |-- XLBaseTest
    |   |   |-- XLAppDelegate.h
    |   |   |-- XLAppDelegate.m
    |   |   |-- XLBaseTest-Info.plist
    |   |   |-- XLBaseTest-Prefix.pch
    |   |   |-- XLViewController.h
    |   |   |-- XLViewController.m
    |   |   |-- main.m
    |-- XLBaseTest(存放自定义组件)
        |-- Assets(资源文件)
        |-- Classes(存放自定义组件代码)
            |-- ReplaceMe.m
            |-- XLTestViewController.h
            |-- XLTestViewController.m

注:每次修改XLBaseTest下文件,在终端cd到Example下执行pod install

podspec文件

Pod::Spec.new do |s|

  s.name             = 'XLBaseTest'
  s.version          = '0.1.0'
  s.summary          = 'A short description of XLBaseTest.'

  s.description      = <<-DESC
TODO: Add long description of the pod here.
                       DESC
                       
  s.homepage         = 'https://github.com/XL/XLBaseTest'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'XL' => 'xie20280@outlook.com' }
  s.source           = { :git => 'https://github.com/Sam/XLBaseTest.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '9.0'
  
  s.source_files = 'XLBaseTest/Classes/**/*'

  # s.resource_bundles = {
  #   'XLBaseTest' => ['XLBaseTest/Assets/*.png']
  # }

  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
  # s.dependency 'AFNetworking', '~> 2.3'

end

详情可参考:CocoaPods - podspec

注:

1、每次修改podspec文件,请在终端cd到Example下执行pod install

2、s.source修改为远端仓库地址;

3、若引用有其他私有库,则需要在PodFile文件中添加私有库来源,如下。

source 'https://gitee.com/Exie/xlspecs.git'

上传代码

1、git add .
2、git commit -m 'XXX' (XXX 提交注释)
3、git remote add origin [URL] (URL 私有库地址)
4、git pull
5、git push origin master
6、git tag '0.1.0'
7、git push --tags

上传spec文件

1、pod repo add XLSpecs [URL] 
2、pod lib lint
3、pod spec lint
4、pod repo push XLSpecs XLBaseTest.podspec

注:

1、XLSpecs:本地仓库管理文件夹名称;

2、[URL]:远端版本管理仓库,如:gitee.com/Exie/xlspec…

3、repo add 成功后可在[.cocoapods]文件夹下看到对应的私有库;

4、repo push 成功后可以通过 pod search XLBaseTest ,搜索到上传的库。

使用

修改项目podfile文件

1、新增source为私有仓库的地址;

2、添加需要导入的私有库,如:XLBaseTest。

platform :ios, '9.0'
use_frameworks!

source 'https://github.com/CocoaPods/Specs.git'        # 官方仓库地址
source 'https://gitee.com/Exie/xlspecs.git'            # 私有仓库地址

target 'test_pod' do
    pod 'XLBaseTest'
    
end

扩展 framework 私有库创建

1、创建framework项目;

2、通过脚本生成framework,点击项目target,选择Build Phases,点击+,选择New Run Script Phases,添加脚本

3、分别编译模拟器和真机,可以自动打开framework所在文件夹;

4、通过执行pod spec create [XX],创建podspec文件,[XX]表示私有库名称;

5、参考上文[podspec]修改相关信息,设置framework如下:

s.vendored_frameworks = 'Products/XXX.framework'

注:Products/XXX.framework中,

Products是framework脚本中保存framework的文件夹;

XXX表示framework名称。

遇到问题可参考 iOS 私有库问题随笔,或者私信,相互探讨。