iOS開發之CocoaPods的使用


你開發iOS的方式還是石器時代嗎?在這個世界上並不是所有的軟件開發人員都是碼農。在這個世界上有很多的geek存在他們為這個語言的發展做出了很大的貢獻。現在隨着iOS開發者的曾多也就出現了iOS程序猿提供的依賴管理工具或者也可以叫中央代碼倉庫這個工具叫做Cocoapods。

iOS石器時代的開發方式

我們在iOS開發中會經常用到很多第三方的開源庫,從而簡便我們的開發 github上提供了非常多的庫給我們使用。我們經常是這么去使用的首先下載下我們所需的庫文件然后手動的添加到我們自己的工程文件里。這樣做的缺點是你的這個庫和版本控制失去了聯系如果庫升級你根本不知道。當然國內開發者很多都在用老版本的庫。更本不知道庫還有升級這么一說。要是你真心覺得第三方庫沒有更新的必要的話這篇blog你也就沒有看下去的必要了。也有的人更新第三方庫的方式是庫更新了重新再做copy一遍。

CocoaPods的使用

CocoaPods是Objective-C項目中最好用的第三方依賴包管理工具.你要做的很簡單只要安裝好后在自己的iOS項目下面建立一個podfile配置文件,在里面協商你要那些庫CocoaPods就會幫你搞定一切。

CocoaPods的安裝和使用

好了廢話就說這么多了下面我們進入正題CocoaPods是ruby gem所以安裝起來會非常簡單蘋果自帶ruby.首先你得ruby -v 和gem -v 去查看下你得版本如果低於2.0得話會安裝得時候報錯

安裝
 $ sudo gem install cocoapods
$ pod setup

這樣就安裝結束了。然后就可以使用了。

查找

你可以通過

 pod search XXXX(來查找你所需要得庫)

比如查找一個我們最常用得json庫

~  pod search json
-> AFJSONPRequestOperation (1.0.0)
AFNetworking Extension for the JSONP format.
pod 'AFJSONPRequestOperation', '~> 1.0.0'
- Homepage: https://github.com/acerbetti/AFJSONPRequestOperation
- Source:   https://github.com/acerbetti/AFJSONPRequestOperation.git
- Versions: 1.0.0 [master repo]
-> AFJSONRPCClient (0.4.0)
A JSON-RPC client build on AFNetworking.
pod 'AFJSONRPCClient', '~> 0.4.0'
- Homepage: https://github.com/AFNetworking/AFJSONRPCClient
- Source:   https://github.com/AFNetworking/AFJSONRPCClient.git
- Versions: 0.4.0, 0.3.1, 0.3.0 [master repo]
-> AnyJSON (0.1.0)
Encode / Decode JSON by any means possible.
pod 'AnyJSON', '~> 0.1.0'
- Homepage: https://github.com/mattt/AnyJSON
- Source:   https://github.com/mattt/AnyJSON.git
- Versions: 0.1.0, 0.0.1 [master repo]
-> ARSafeJSON (0.0.7)
ARSafeJSON is a small library that will strip all occurences of NSNull from a
JSON of any depth.
pod 'ARSafeJSON', '~> 0.0.7'
- Homepage: https://bitbucket.org/antoine_r/arsafejson
- Source:   https://bitbucket.org/antoine_r/arsafejson.git
- Versions: 0.0.7, 0.0.3 [master repo]
-> Collection-JSON-ObjC (1.0.1)
Collection+JSON parser for Objective-C.
pod 'Collection-JSON-ObjC', '~> 1.0.1'
- Homepage: https://github.com/chrissearle/Collection-JSON-ObjC
- Source:   https://github.com/chrissearle/Collection-JSON-ObjC.git
- Versions: 1.0.1 [master repo]
-> FXJSON (1.1)
Lightweight, ARC-friendly JSON library, supporting both DOM and SAX style
parsing.
pod 'FXJSON', '~> 1.1'
- Homepage: https://github.com/nicklockwood/FXJSON
- Source:   https://github.com/nicklockwood/FXJSON.git
- Versions: 1.1 [master repo]
-> GDJson (0.3.0)
J2ObjC implementation of GWT Elemental JSON library.
pod 'GDJson', '~> 0.3.0'
- Homepage: https://github.com/goodow/realtime
- Source:   https://github.com/goodow/GDJson.git
- Versions: 0.3.0 [master repo]
-> jsoncpp (0.6.0.rc2)
jsoncpp is an implementation of a JSON (http://json.org) reader and writer in
C++.
pod 'jsoncpp', '~> 0.6.0.rc2'
- Homepage: http://jsoncpp.sourceforge.net/
- Source:   https://svn.code.sf.net/p/jsoncpp/code/tags/jsoncpp/0.6.0-rc2/
- Versions: 0.6.0.rc2 [master repo]
-> JSONJoy (0.0.2)
Makes JSON a joy to use
pod 'JSONJoy', '~> 0.0.2'
- Homepage: https://github.com/daltoniam/JSONJoy
- Source:   https://github.com/daltoniam/JSONJoy.git
- Versions: 0.0.2, 0.0.1 [master repo]
-> JSONKit (1.5pre)
A Very High Performance Objective-C JSON Library.
pod 'JSONKit', '~> 1.5pre'
- Homepage: https://github.com/johnezang/JSONKit
- Source:   https://github.com/johnezang/JSONKit.git
- Versions: 1.5pre, 1.4 [master repo]
-> JsonLite (1.1.0)
High performance and low memory footprint JSON parser for mobile/embedded
systems
pod 'JsonLite', '~> 1.1.0'
- Homepage: https://github.com/amamchur/jsonlite
- Source:   https://github.com/amamchur/jsonlite.git
- Versions: 1.1.0 [master repo]
-> JSONModel (0.10.0)
Magical Data Modelling Framework for JSON. Create rapidly powerful, atomic
and smart data model classes.
pod 'JSONModel', '~> 0.10.0'
- Homepage: http://www.jsonmodel.com
- Source:   https://github.com/icanzilb/JSONModel.git
- Versions: 0.10.0, 0.9.3, 0.9.2, 0.9.1, 0.9.0, 0.8.4, 0.8.2, 0.8.1 [master
repo]
-> JSONSyntaxHighlight (1.0.0)
Add syntax highlighting to JSON objects in Objective C for both Cocoa and iOS
without using HTML.
pod 'JSONSyntaxHighlight', '~> 1.0.0'
- Homepage: https://github.com/bahamas10/JSONSyntaxHighlight
- Source:   https://github.com/bahamas10/JSONSyntaxHighlight.git
- Versions: 1.0.0 [master repo]
-> MTJSONDictionary (0.0.4)
An NSDictionary category for when you're working with it converting to/from
JSON. DEPRECATED, use MTJSONUtils instead.
pod 'MTJSONDictionary', '~> 0.0.4'
- Homepage: https://github.com/mysterioustrousers/MTJSONDictionary.git
- Source:   https://github.com/mysterioustrousers/MTJSONDictionary.git
- Versions: 0.0.4, 0.0.3, 0.0.2 [master repo]
-> MTJSONUtils (0.1.1)
An NSObject category for working with JSON.
pod 'MTJSONUtils', '~> 0.1.1'
- Homepage: https://github.com/mysterioustrousers/MTJSONUtils.git
- Source:   https://github.com/mysterioustrousers/MTJSONUtils.git
- Versions: 0.1.1, 0.1.0, 0.0.1 [master repo]
-> PKJSONSocket (0.0.3)
Simplified socket networking based on CocoaAsynSocket. Enabling direct JSON
messaging between devices.
pod 'PKJSONSocket', '~> 0.0.3'
- Homepage: https://github.com/pkluz/PKJSONSocket
- Source:   https://github.com/pkluz/PKJSONSocket.git
- Versions: 0.0.3, 0.0.2, 0.0.1 [master repo]
-> RDHJSONObjectSerialisation (0.5.0)
Simple JSON serialisation for any custom NSObject based on its declared
properties.
pod 'RDHJSONObjectSerialisation', '~> 0.5.0'
- Homepage: https://github.com/rhodgkins/RDHJSONObjectSerialisation
- Source:   https://github.com/rhodgkins/RDHJSONObjectSerialisation.git
- Versions: 0.5.0 [master repo]
-> SBJson (3.2)
This library implements strict JSON parsing and generation in Objective-C.
pod 'SBJson', '~> 3.2'
- Homepage: http://stig.github.com/json-framework/
- Source:   https://github.com/stig/json-framework.git
- Versions: 3.2, 3.1.1, 3.1, 3.0.4, 2.2.3 [master repo]
-> SDJSONPrettyPrint (0.0.1)
Produces human-friendly JSON, which you can use for logging or debugging
purposes.
pod 'SDJSONPrettyPrint', '~> 0.0.1'
- Homepage: https://github.com/tyrone-sudeium/SDJSONPrettyPrint
- Source:   https://github.com/tyrone-sudeium/SDJSONPrettyPrint.git
- Versions: 0.0.1 [master repo]
-> SEJSONViewController (0.1.0)
Easily browse JSON content.
pod 'SEJSONViewController', '~> 0.1.0'
- Homepage: https://github.com/SergioEstevao/SEJSONControllerView
- Source:   https://github.com/SergioEstevao/SEJSONViewController.git
- Versions: 0.1.0 [master repo]
-> SVJsonSchemaValidator (0.0.7)
Simple JSON Validator for Objective-C.
pod 'SVJsonSchemaValidator', '~> 0.0.7'
- Homepage: https://bitbucket.org/nut_code_monkey/jsonschemavalidator
- Source:
https://nut_code_monkey@bitbucket.org/nut_code_monkey/jsonschemavalidator.git
- Versions: 0.0.7, 0.0.6, 0.0.4, 0.0.3, 0.0.2 [master repo]
-> TouchJSON (1.1)
A humane JSON Objective-C un-framework. (TouchJSON has been deprecated - see
README).
pod 'TouchJSON', '~> 1.1'
- Homepage: https://github.com/touchcode/
- Source:   https://github.com/TouchCode/TouchJSON.git
- Versions: 1.1, 1.0 [master repo]
-> VeriJSON (0.1.1)
An Objective-C library for verifying JSON against a pattern-based schema.
pod 'VeriJSON', '~> 0.1.1'
- Homepage: https://bitbucket.org/dcutting/verijson
- Source:   https://bitbucket.org/dcutting/verijson
- Versions: 0.1.1, 0.1.0 [master repo]

➜ ~

使用

首先通過終端進入工程文件建立podfile文件

  touch Podfile
vim Podfile

然后你可以在podfile文件中添加你所需要的庫的名稱和版本

     platform :ios, '6.0'
pod 'JSONKit', '~> 1.5pre'
wq!(保存退出)

然后在執行

   pod install

注意 你每次更改完Podfile文件后都要執行pod install
做完這些后你就可以在你的工程目錄下看見一個后綴為.xcworkspace你寫項目打開這個就OK了。打開看以后會發現其實cocoapods做了一個事情就是他把第三方庫都編譯成了一個庫文件。然后你的項目中去包含頭文件,libPods.a被作為framework集成到目標項目。Pods.xcconfig 中配置了XCode編譯的查找路徑,通過這種方式將所有的第三方包來引入到項目中。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM