zoukankan      html  css  js  c++  java
  • iOS开发~CocoaPods使用详细说明

     

    一、概要

    iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来方便的统一管理这些第三方库。

    二、安装

    由于网上的教程基本都大同小异,但细节之处还不是很完善,所以借机会在这里补充下:

    注:要使用CocoaPods,那就要下载安装它,而下载安装CocoaPods需要Ruby环境

    1、Ruby环境搭建

    当前安装环境为Mac mini 10.8.5。Mac  OS本身自带Ruby,但还是更新一下保险,因为我第一次安装在没有更新Ruby的情况下就失败了。

    a 查看下当前ruby版本:打开终端输入 ruby -v(确实安装了,不过用这个版本接下来工作失败了,所以更新下ruby)

    1. ritekiMac-mini:PodTest lucky$ ruby -v  
    2. ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin12.0]  
    3. ritekiMac-mini:PodTest lucky$   

    b 更新ruby

    终端输入如下命令(把Ruby镜像指向taobao,避免被墙,你懂得)

    gem sources --remove https://rubygems.org/ 
    gem sources -a https://ruby.taobao.org/ 
    gem sources -l  (用来检查使用替换镜像位置成功)

    1. ritekiMac-mini:~ lucky$ gem sources --remove https://rubygems.org/  
    2. https://rubygems.org/ removed from sources  
    3. ritekiMac-mini:~ lucky$ gem sources -a https://ruby.taobao.org/  
    4. https://ruby.taobao.org/ added to sources  
    5. ritekiMac-mini:~ lucky$ gem sources -l  
    6. *** CURRENT SOURCES ***  
    2、下载安装CocoaPods

    终端输入:sudo gem install cocoapods 

    1. ritekiMac-mini:~ lucky$ sudo gem install cocoapods  
    2.   
    3. CHANGELOG:  
    4.   
    5. ## 0.32.1  
    6.   
    7. ##### Bug Fixes  
    8.   
    9. * Fixed the Podfile `default_subspec` attribute in nested subspecs.    
    10.   [Fabio Pelosin][irrationalfab]  
    11.   [#2050](https://github.com/CocoaPods/CocoaPods/issues/2050)  
    12.   
    13.   
    14. Successfully installed cocoapods-0.32.1  
    15. Installing ri documentation for cocoapods-0.32.1  
    16. /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rdoc/rdoc.rb:280: warning: conflicting chdir during another chdir block  
    17. /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rdoc/rdoc.rb:287: warning: conflicting chdir during another chdir block  
    18. Done installing documentation for cocoapods after 10 seconds  
    19. 1 gem installed  

    这样就下载安装好了CocoaPods

    3、使用CocoaPods

    a  新建一个项目,名字PodTest

    b  终端中,cd到项目总目录(注意:包含PodTest文件夹、PodTest.xcodeproj、PodTestTest的那个总目录)

     
    1. cd /Users/lucky/Desktop/PodTest   

    c  建立Podfile(配置文件)

    接着上一步,终端输入 vim Podfile

    键盘输入 i,进入编辑模式,输入

    platform :ios, '7.0' 
    pod 'MBProgressHUD', '~> 0.8'

    然后按Esc,并且输入“ :”号进入vim命令模式,然后在冒号后边输入wq

    注意:键盘输入 :后,才能输入wq。回车后发现PodTest项目总目录中多一个Podfile文件

    激动人心的时刻到了:确定终端cd到项目总目录,然后输入 pod install,等待一会,大约3分钟。

    查看项目根目录:

    注意:现在打开项目不是点击 PodTest.xodeproj了,而是点击 PodTest.xcworkspace

    打开项目后看到项目结构并且测试一下:

    运行结果:

    补充:

    1、CocoaPods的基本安装及使用都详细的说明了,但还有一些补充,当需要同时导入多个第三方时候怎么办 ?

    这就需要修改Podfile了,就是用vim编辑的那个保存在项目根目录中的文件,修改完了Podfile文件,需要重新执行一次pod install命令。

    例如:

    platform :ios

    pod 'JSONKit',       '~> 1.4'

    pod 'AFNetworking',  '~> 2.0'

     

    2、CocoaPods可以查找你想要的第三方库

    终端输入命令:pod search UI

    疯了了,我怎么查找这么大众的关键字,好多库~~

    然后重新编辑Podfile文件,按照之前的步骤,把更多的库都导入项目!

    本博客盗窃自阿福的博客,地址:http://blog.csdn.net/lizhongfu2013/article/details/26384029

    ============================我是分割线==================================

     CocoaPods更新了一下,然后:出现下面的警告

    ex:

    The dependency `` is not used in any concrete target
    The dependency `AFNetworking ` is not used in any concrete target

    CocoaPods更新了一下,然后:

    sudo gem install cocoapods --pre

    安装cocoapods的预览版本,就会更新下来新的1.0.0.beta.2版本,如下所示:

    Successfully installed cocoapods-1.0.0.beta.2
    Parsing documentation for cocoapods-1.0.0.beta.2

    很高兴啊,更新了新的版本,然而pod install就出错了,悲了个剧!出错如下:

    Updating local specs repositories
    Analyzing dependencies
    [!] The dependency `FMDB (~> 2.3)` is not used in any concrete target.
    The dependency `SDWebImage (~> 3.6)` is not used in any concrete target.
    The dependency `AFNetworking (~> 2.3.0)` is not used in any concrete target.
    The dependency `DACircularProgress (~> 2.2.0)` is not used in any concrete target.
    The dependency `MBProgressHUD (~> 0.8)` is not used in any concrete target.
    The dependency `PSTCollectionView (~> 1.2.1)` is not used in any concrete target.
    The dependency `HPGrowingTextView (~> 1.1)` is not used in any concrete target.
    The dependency `ProtocolBuffers (= 1.9.3)` is not used in any concrete target.
    The dependency `leveldb-library (~> 1.18.1)` is not used in any concrete target.
    The dependency `SCLAlertView-Objective-C (~> 0.7.5)` is not used in any concrete target.
    The dependency `MWPhotoBrowser (~> 1.4.1)` is not used in any concrete target.
    The dependency `MMMarkdown (~> 0.5)` is not used in any concrete target.
    The dependency `MJExtension (~> 2.5.16)` is not used in any concrete target.
    The dependency `MJRefresh (~> 2.4.12)` is not used in any concrete target.
    The dependency `Masonry (~> 0.6.3)` is not used in any concrete target.

    我用的三方库比较多,挺长的,出这个错是告诉我们我们所用的库没有指定target,它不知道用在哪里,所以就给报错了,然后我去了cocoapods的官网看了下,cocoapods官网地址


    官网是这样给推荐的: 
    在创建Podfile的时候,用这种格式使用,

    platform :ios, '8.0'
    use_frameworks!
    
    target 'MyApp' do
      pod 'AFNetworking', '~> 2.6'
      pod 'ORStackView', '~> 3.0'
      pod 'SwiftyJSON', '~> 2.3'
    end

    里面的 MyApp 记得替换为自己攻城里面的target。这样就基本OK了,执行pod install / pod update 就都可以了。

  • 相关阅读:
    WPF Step By Step -基础知识介绍
    WPF Step By Step 系列
    设计模式的六大原则
    Java实现二维码生成的方法
    Java 导出Excel
    解析图书 XML
    Maven搭建Spring+SpringMVC+Mybatis+Shiro项目详解
    springboot配置文件的所有属性
    SpringBoot中 application.yml /application.properties常用配置介绍
    Linux 系统目录结构
  • 原文地址:https://www.cnblogs.com/496668219long/p/5135734.html
Copyright © 2011-2022 走看看