iOS cocoapod私有库创建

212 阅读2分钟

1 私有库创建

1.1 创建初始项目

下文以网络组件为例,使用XXNetworkBase作为项目示例名称,终端输入命令创建库的初始项目:

pod lib create XXNetworkBase

执行结果:

What platform do you want to use?? [ iOS / macOS ]
​
# 直接回车或者输入iOS选择运行平台为iOS:
​
> iOS
​
What language do you want to use?? [ Swift / ObjC ]
​
# 直接回车或者输入Swift选择项目语言为Swift:
​
> Swift
​
Would you like to include a demo application with your library? [ Yes / No ]
​
# 直接回车或者输入Yes选择创建示例项目:
​
> Yes
​
Which testing frameworks will you use? [ Quick / None ]
​
# 输入None不使用三方测试框架:
​
> None
​
Would you like to do view based testing? [ Yes / No ]
​
# 输入No不使用视图测试框架:
​
> No

完成初始项目创建及基本配置之后,Xcode会自动打开项目工作区。

1.2 修改配置文件

在初始项目中,找到“XXNetworkBase.podspec”文件,该文件即为仓库配置文件。修改为如下配置:

#
# Be sure to run `pod lib lint XXNetworkBase.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
  s.name             = 'XXNetworkBase'
  s.version          = '0.1.0'
  s.summary          = 'XXNetworkBase xxxxxxxxxxx'# This description is used to generate tags and improve search results.
#   * Think: What does it do? Why did you write it? What is the focus?
#   * Try to keep it short, snappy and to the point.
#   * Write the description between the DESC delimiters below.
#   * Finally, don't worry about the indent, CocoaPods strips it!
​
  s.description      = 'XXNetworkBase xxxxxx'
  s.homepage         = 'https://xxx/git/ios_dev/XXNetworkBase'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'xxx' => 'xxx@xxx.com' }
  s.source           = { :git => ''https://xxx/git/ios_dev/XXNetworkBase.git', :tag => s.version.to_s }
​
  s.ios.deployment_target = '10.0'
  s.swift_version = '5.0'
  s.static_framework = true
​
  s.subspec 'Core' do |c|
      c.resource_bundles = {
          'XXNetworkBaseResource' => ['XXNetworkBase/Assets/**/*.{xib,xcdatamodeld,png,jpg,html,js,svg,xcassets}'] 
      }
      c.source_files = 'XXNetworkBase/Classes/Core/**/*'
      c.public_header_files = ['XXNetworkBase/Classes/Core/*.h','XXNetworkBase/Classes/Core/**/*.h']
      c.dependency 'xxxxxxxx'
  end
​
  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
  # s.dependency 'AFNetworking', '~> 2.3'
end

1.3 添加代码文件

  1. 根据配置文件内容,我们需要在项目中创建XXNetworkBase->Classes->Core结构的目录,并在该目录下创建“HelloWorld.swift”代码文件。
  2. 打开终端,输入命令,进入示例工程主目录: cd /Users/xxx/Documents/Workspaces/XXNetworkBase/Example/
  3. 编辑Podfile:
#可以是公司的私有仓库,可以是清华源等
#清华源,可能存在第三方库不是最新版本的情况
#source 'https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git'
source 'xxx.git'
​
use_frameworks!
​
platform :ios, '10.0'
​
target 'XXNetworkBase_Example' do
  pod 'XXNetworkBase', :path => '../'
​
  target 'XXNetworkBase_Tests' do
    inherit! :search_paths
    
  end
end
​
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
  1. 安装本地开发库:pod install

pod install后出现很多下面的警告

[!] [Xcodeproj] Generated duplicate UUIDs:这是因为私有库中出现了很多重复的文件,虽然他们处于不同的文件夹下. 同时我还发现如果没有指明resource_bundles的扩展名,也会出现这个问题{xib,xcdatamodeld,png,svg,jpg,xcassets,html,js,css,json,glb,xodr,txt}

  • 解决办法1

在项目正常运行且没有强迫症的情况下,可以在podfile中加入install! 'cocoapods', :deterministic_uuids => false

  • 解决办法2

也可在警告中的最后一行,找出文件名,去xcode中的全局搜索文件名看看,是否重复,想办法解决。