最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[记录]使用Cocoapods管理iOS项目的第三方库

Cocoapods crifan 1992浏览 0评论

[记录]使用Cocoapods管理iOS项目的第三方库

之前已经:

[已解决]Mac中尝试安装pod结果出错:-bash: pod: command not found

现在去试试使用cocoapods:

参考:

如何使用CocoaPods – snowell – SegmentFault

CocoaPods的安装使用和常见问题 – 简书

CocoaPods.org

先去搜需要的库:

licrifandeMacBook-Pro:QorosSales crifan$ pod search chart
-> ANDLineChartView (0.2.2)
   ANDLineChartView is easy to use view-based class for displaying animated line chart.
   pod ‘ANDLineChartView’, ‘~> 0.2.2’
   – Versions: 0.2.2, 0.2.1, 0.2, 0.1.1, 0.1.0 [master repo]
。。。
-> Charts (2.2.4)
   Charts is a powerful & easy to use chart library for iOS, tvOS and OSX (and Android)
   pod ‘Charts’, ‘~> 2.2.4’
   – Versions: 2.2.4, 2.2.3, 2.2.2, 2.2.1, 2.1.6, 2.1.5, 2.1.4, 2.1.4a, 2.1.3, 2.1.2, 2.1.0, 2.0.9 [master repo]
   – Subspecs:
     – Charts/Core (2.2.4)
     – Charts/Realm (2.2.4)
。。。
-> EChart (0.1.1)
   iOS/iPhone/iPad Chart, Graph. Event handling and animation supported.
   pod ‘EChart’, ‘~> 0.1.1’
   – Versions: 0.1.1, 0.0.1 [master repo]
。。。
-> ios-linechart-2 (1.3.4)
   Interactive line charts / plots for the simplicity-loving iOS developer. This was forked from
   pod ‘ios-linechart-2’, ‘~> 1.3.4’
   – Versions: 1.3.4, 1.3.3, 1.3.2 [master repo]
licrifandeMacBook-Pro:QorosSales crifan$

其中:

-> Charts (2.2.4)
   Charts is a powerful & easy to use chart library for iOS, tvOS and OSX (and Android)
   pod ‘Charts’, ‘~> 2.2.4’
   – Versions: 2.2.4, 2.2.3, 2.2.2, 2.2.1, 2.1.6, 2.1.5, 2.1.4, 2.1.4a, 2.1.3, 2.1.2, 2.1.0, 2.0.9 [master repo]
   – Subspecs:
     – Charts/Core (2.2.4)
     – Charts/Realm (2.2.4)

是我们要的:

pod ‘Charts’, ‘~> 2.2.4’

所以去

项目根目录中创建Podfile:

后,填写内容为:

platform :ios, ‘8.0’
use_frameworks!
target ‘QorosSales’ do
  pod ‘Charts’, ‘~> 2.2.4’
end

去试试:

pod init

licrifandeMacBook-Pro:QorosSales crifan$ pwd
/Users/crifan/dev/dev_root/daryun/Projects/xxx
licrifandeMacBook-Pro:QorosSales crifan$ ls -l
total 8
-rw-r–r–  1 crifan  staff   91  5 12 16:01 Podfile
drwxr-xr-x  7 crifan  staff  238  5 12 15:40 QorosSales
drwxr-xr-x  5 crifan  staff  170  5 12 15:42 QorosSales.xcodeproj
drwxr-xr-x  4 crifan  staff  136  5 12 15:40 QorosSalesTests
drwxr-xr-x  4 crifan  staff  136  5 12 15:40 QorosSalesUITests
licrifandeMacBook-Pro:QorosSales crifan$ rm Podfile
licrifandeMacBook-Pro:QorosSales crifan$ pod init
licrifandeMacBook-Pro:QorosSales crifan$ ls -l
total 8
-rw-r–r–  1 crifan  staff  252  5 12 16:04 Podfile
drwxr-xr-x  7 crifan  staff  238  5 12 15:40 QorosSales
drwxr-xr-x  5 crifan  staff  170  5 12 15:42 QorosSales.xcodeproj
drwxr-xr-x  4 crifan  staff  136  5 12 15:40 QorosSalesTests
drwxr-xr-x  4 crifan  staff  136  5 12 15:40 QorosSalesUITests
licrifandeMacBook-Pro:QorosSales crifan$ cat Podfile
# Uncomment this line to define a global platform for your project
# platform :ios, ‘8.0’
# Uncomment this line if you’re using Swift
# use_frameworks!
target ‘QorosSales’ do
end
target ‘QorosSalesTests’ do
end
target ‘QorosSalesUITests’ do
end
licrifandeMacBook-Pro:QorosSales crifan$

默认的内容为:

# Uncomment this line to define a global platform for your project
# platform :ios, ‘8.0’
# Uncomment this line if you’re using Swift
# use_frameworks!
target ‘QorosSales’ do
end
target ‘QorosSalesTests’ do
end
target ‘QorosSalesUITests’ do
end

再去改为:

# Uncomment this line to define a global platform for your project
platform :ios, ‘8.0’
# Uncomment this line if you’re using Swift
use_frameworks!
target ‘QorosSales’ do
  pod ‘Charts’, ‘~> 2.2.4’
end
target ‘QorosSalesTests’ do
end
target ‘QorosSalesUITests’ do
end

然后去下载安装对应的库:

licrifandeMacBook-Pro:QorosSales crifan$ pod install
Updating local specs repositories
CocoaPods 1.0.0 is available.
To update use: `gem install cocoapods`
Until we reach version 1.0 the features of CocoaPods can and will change.
We strongly recommend that you use the latest version at all times.
For more information see http://blog.cocoapods.org
and the CHANGELOG for this version http://git.io/BaH8pQ.
Analyzing dependencies
Downloading dependencies
Installing Charts (2.2.4)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use `QorosSales.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

看到提示建议安装新版本cocoapods,所以打算去升级

参考:

CocoaPods详解之—-使用篇 – 王中周的个人博客 – 博客频道 – CSDN.NET

还是去用之前的命令去升级:

sudo gem install -n /usr/local/bin cocoapods

licrifandeMacBook-Pro:QorosSales crifan$ sudo gem install -n /usr/local/bin cocoapods
Password:
Fetching: cocoapods-core-1.0.0.gem (100%)
Successfully installed cocoapods-core-1.0.0
Fetching: claide-1.0.0.gem (100%)
Successfully installed claide-1.0.0
。。。
Parsing documentation for cocoapods-1.0.0
Installing ri documentation for cocoapods-1.0.0
12 gems installed
licrifandeMacBook-Pro:QorosSales crifan$

然后去打开对应的xcworkspace:

命令行中:open xxx.xcworkspace

Finder中:双击:xxx.xcworkspace

注:

对应的,也看到了。

pod install

后,除了安装了对应的库之外,还创建了那个:Podfile.lock

内容为:

PODS:
  – Charts (2.2.4):
    – Charts/Core (= 2.2.4)
  – Charts/Core (2.2.4)
DEPENDENCIES:
  – Charts (~> 2.2.4)
SPEC CHECKSUMS:
  Charts: a9e735c83c6d376ade20f6612dcf0ba217394a49
COCOAPODS: 0.39.0

以及多出了Pod文件夹:

其中有各种文件和已下载的库的源码:

Charts的

打开后,除了本身已存在的原先项目,多出了个Pod子项目:

这个是原先的项目:

这个是Podfile

参考了:

CocoaPods详解之—-使用篇 – 王中周的个人博客 – 博客频道 – CSDN.NET

对于工程发生的变化,有几点需要说明:

  • 第三方库会被编译成静态库供我们正真的工程使用

CocoaPods会将所有的第三方库以target的方式组成一个名为Pods的工程,该工程就放在刚才新生成的Pods目录下。整个第三方库工程会生成一个名称为libPods.a的静态库提供给我们自己的CocoaPodsTest工程使用。

另外:

如果嫌弃每次pod install太慢,则可以去禁止掉默认的,每次都会更新repo的动作:

pod install –verbose –no-repo-update

转载请注明:在路上 » [记录]使用Cocoapods管理iOS项目的第三方库

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.207 seconds, using 22.21MB memory