第一次使用cocoaPods

时间:2022-03-06 01:48:22

原文链接:http://www.jianshu.com/p/d5a9e4bee015

CocoaPods是什么?

CocoaPods 是开发 OS X 和 iOS 应用程序的一个第三方库的依赖管理工具。利用 CocoaPods,可以定义自己的依赖关系 (称作 pods),并且随着时间的变化,以及在整个开发环境中对第三方库的版本管理非常方便。

CocoaPods 背后的理念主要体现在两个方面。
首先,在工程中引入第三方代码会涉及到许多内容。对开发者来说,工程文件的配置会让人很沮丧。在配置 build phases 和 linker flags 过程中,会引起许多人为因素的错误。CocoaPods 简化了这一切,它能够自动配置编译选项。

通过 CocoaPods,可以很方便的查找到新的第三方库。

CocoaPods是用 Ruby 写的,想要使用它首先要有Ruby环境.
RubyGems简称gems,是一个用于对Ruby组件进行打包的Ruby打包系统

个人总结的安装步骤(看了好多相关的博客)

将ruby的默认源替换为淘宝的RubyGems镜像
因为国内网络原因,导致rubygems.org存放在亚马逊上面的资源容易连接失败
gem sources --remove https://rubygems.org/gem sources -a https://ruby.taobao.org/
查看此时的源
bogon:~ liyilin$ gem source -l
输出结果
*** CURRENT SOURCES ***https://ruby.taobao.org/

之后就可以更新Mac的gem,gem是管理Ruby库和程序的标准包
bogon:~ liyilin$ sudo gem update --system
终端提示
Updating rubygems-updateERROR: While executing gem ... (Errno::EPERM) Operation not permitted - /usr/bin/update_rubygems
上网搜索后得知
Mac OS X 10.11后不能使用sudo gem update --system了
需要替换成:sudo gem update -n /usr/local/bin —system
bogon:~ liyilin$ sudo gem update -n /usr/local/bin —system
终端提示貌似没有可以更新的内容
Updating installed gemsNothing to update

接下来安装CocoaPods
bogon:~ liyilin$ sudo gem install -n /usr/local/bin cocoa pods
终端提示成功了...
Successfully installed cocoapods-1.0.1Parsing documentation for cocoapods-1.0.11 gem installed

使用CocoaPods查找需要的第三方框架(就拿AFNetworking试试吧)
bogon:~ liyilin$ pod search AFNetworking
终端提示如下,并卡住了!
Setting up CocoaPods master repo
上网搜索后得知Cocoapods在将它的信息下载到 ~/.cocoapods里
然后我就前往这个文件夹,不断用快捷键预览它的大小
下载速度太慢了,我想先去玩几把守望先锋吧,也不能干等着吧玩了五局定级赛(2胜3负),还没下载完.那我去搓几把炉石吧.

炉石任务正刷着的时候,终端显示下载完了,但是还有一个警告
Setup completed[!] Unable to find a pod with name, author, summary, or description matching 'AFNetworking'
上网搜索后得知要删除一个文件
bogon:~ liyilin$ rm ~/Library/Caches/CocoaPods/search_index.json
然后我再一次搜索AFNetworking
bogon:~ liyilin$ pod search AFNetworking
终端打印如下:

Creating search index for spec repo 'master'.. Done!
   - Homepage: https://github.com/steipete/AFDownloadRequestOperation    - Source: https://github.com/steipete/AFDownloadRequestOperation.git    - Versions: 2.0.1, 2.0.0, 1.1.0, 1.0.0, 0.0.2, 0.0.1 [master repo] -> AFgzipRequestSerializer (0.0.2)    gzip Request Serializer for AFNetworking
   pod 'AFgzipRequestSerializer', '~> 0.0.2'
   - Homepage: https://github.com/AFNetworking/AFgzipRequestSerializer    - Source: https://github.com/AFNetworking/AFgzipRequestSerializer.git    - Versions: 0.0.2, 0.0.1 [master repo] -> AFHARchiver (0.2.2)    An AFNetworking extension to automatically generate a HTTP Archive file of
   all of your network requests.
   pod 'AFHARchiver', '~> 0.2.2'
   - Homepage: https://github.com/mutualmobile/AFHARchiver    - Source: https://github.com/mutualmobile/AFHARchiver.git    - Versions: 0.2.2, 0.2.1, 0.2.0, 0.1.0, 0.0.2, 0.0.1 [master repo] -> AFHTTPClientLogger (0.7.0)    AFNetworking Extension for request logging.
   pod 'AFHTTPClientLogger', '~> 0.7.0'
   - Homepage: https://github.com/jparise/AFHTTPClientLogger    - Source: https://github.com/jparise/AFHTTPClientLogger.git    - Versions: 0.7.0, 0.6.1, 0.6.0, 0.5.0 [master repo] -> AFHTTPFileUpdateOperation (0.0.1)    AFNetworking extenstion for updating file with HTTP header
   "If-Modified-Since" and status code 304.
   pod 'AFHTTPFileUpdateOperation', '~> 0.0.1'
   - Homepage: https://github.com/samnung/AFHTTPFileUpdateOperation    - Source: https://github.com/samnung/AFHTTPFileUpdateOperation.git    - Versions: 0.0.1 [master repo] -> AFHTTPRequestOperationLogger (1.0.0)

应该是搜索出了与AFNetworking相关的内容

我再一次重新搜索AFNetworking,终端显示如下

-> AFNetworking (3.1.0)
   A delightful iOS and OS X networking framework.
   pod 'AFNetworking', '~> 3.1.0'
   - Homepage: https://github.com/AFNetworking/AFNetworking
   - Source:   https://github.com/AFNetworking/AFNetworking.git
   - Versions: 3.1.0, 3.0.4, 3.0.3, 3.0.2, 3.0.1, 3.0.0, 3.0.0-beta.3,
   3.0.0-beta.2, 3.0.0-beta.1, 2.6.3, 2.6.2, 2.6.1, 2.6.0, 2.5.4, 2.5.3, 2.5.2,
   2.5.1, 2.5.0, 2.4.1, 2.4.0, 2.3.1, 2.3.0, 2.2.4, 2.2.3, 2.2.2, 2.2.1, 2.2.0,
   2.1.0, 2.0.3, 2.0.2, 2.0.1, 2.0.0, 2.0.0-RC3, 2.0.0-RC2, 2.0.0-RC1, 1.3.4,
   1.3.3, 1.3.2, 1.3.1, 1.3.0, 1.2.1, 1.2.0, 1.1.0, 1.0.1, 1.0, 1.0RC3, 1.0RC2,
   1.0RC1, 0.10.1, 0.10.0, 0.9.2, 0.9.1, 0.9.0, 0.7.0, 0.5.1 [master repo]
   - Subspecs:
     - AFNetworking/Serialization (3.1.0)
     - AFNetworking/Security (3.1.0)
     - AFNetworking/Reachability (3.1.0)
     - AFNetworking/NSURLSession (3.1.0)
     - AFNetworking/UIKit (3.1.0)

说明CocoaPods确实能用了

用CocoaPods管理项目中的第三方框架
打开你的项目所在的那个文件夹,新建一个叫Podfile的文件
bogon:~ liyilin$ cd /Users/liyilin/Desktop/cocoapodsTest bogon:cocoapodsTest liyilin$ pod init

编辑Podfile文件
bogon:cocoapodsTest liyilin$ vim Podfile
终端显示Podfile内容如下

# Uncomment this line to define a global platform for your project
# platform :ios, '9.0'
target 'cocoapodsTest' do
  # Comment this line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for cocoapodsTest

end
~ ~                                                                               
~ ~                                                                               
~ ~                                                                               
~ ~                                                                               
~ ~                                                                               
~ ~                                                                               
"Podfile" 10L, 257C

其中注释说如果使用Swift就得保留use_frameworks!
输入i进行编辑,终端显示如下,注意最后一行变为-- INSERT --,此时可以编辑

# Uncomment this line to define a global platform for your project
# platform :ios, '9.0'

target 'cocoapodsTest' do
  # Comment this line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for cocoapodsTest

end
~ ~                                                                               
~ ~                                                                               
~ ~                                                                               
~ ~                                                                               
~ ~                                                                               
~ ~                                                                               
-- INSERT --

然后我根据搜索出的AFNetworking的信息,编辑Podfile文件如下:
(去掉了platform前面的#,插入了pod 'AFNetworking', '~> 3.1.0')

# Uncomment this line to define a global platform for your project
 platform :ios, '9.0'

target 'cocoapodsTest' do
  # Comment this line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!
pod 'AFNetworking', '~> 3.1.0'
  # Pods for cocoapodsTest

end
~ ~                                                                               
~ ~                                                                               
~ ~                                                                               
~ ~                                                                               
~ ~                                                                               
~ ~                                                                               
-- INSERT --

然后点击ESC,输入:wq保存并退出

# Uncomment this line to define a global platform for your project
 platform :ios, '9.0'

target 'cocoapodsTest' do
  # Comment this line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!
pod 'AFNetworking', '~> 3.1.0'
  # Pods for cocoapodsTest

end
~ ~                                                                               
~ ~                                                                               
~ ~                                                                               
~ ~                                                                               
~ ~                                                                               
~ ~                                                                               
:wq

接下来为项目导入第三方库和相关依赖库
bogon:cocoapodsTest liyilin$ pod install
终端提示导入成功,并提示你从此使用cocoapodsTest.xcworkspace这个文件来编写项目

Analyzing dependencies
Downloading dependencies
Installing AFNetworking (3.1.0)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and usecocoapodsTest.xcworkspacefor this project from now on.
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total
pod installed.

使用cocoapodsTest.xcworkspace打开项目,目录如下:

第一次使用cocoaPods
屏幕快照 2016-07-24 下午11.52.28.png

之后我参考了AFNetworking在github上的示例,试验了一下,证明AFNetworking导入成功而且能使用.

import UIKit
import AFNetworking

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
        let manager = AFURLSessionManager.init(sessionConfiguration: configuration)
        let URL = NSURL(string: "http://httpbin.org/get")
        let request = NSURLRequest(URL: URL!)
        let dataTask = manager.dataTaskWithRequest(request) { (response, responseObject, error) in

            if error != nil
            {
                print("Error: \(error)")
            }
            else
            {
                print("response->\(response)");
                print("responseObject->\(responseObject)")
            }
        }
        dataTask.resume()

    }
}

调试窗口打印如下:

第一次使用cocoaPods
屏幕快照 2016-07-24 下午11.58.33.png

OVER