美文网首页
CocoaPods概述

CocoaPods概述

作者: dongwenbo | 来源:发表于2017-01-16 09:02 被阅读14次

    CocoaPods是一个Swift和Objective-C包依赖管理工具

    安装

    CocoaPods通过Ruby构建,OS X自带Ruby,RubyGems是一个复杂的软件包管理工具,CocoaPods就是其中的一个软件包。
    假设你已经有了Ruby和RubyGems,执行以下命令

    sudo gem install cocoapods
    

    就是这么简单

    Successfully installed cocoapods-1.1.1
    Parsing documentation for cocoapods-1.1.1
    Done installing documentation for cocoapods after 2 seconds
    1 gem installed
    

    安装好了

    上手

    在你的Xcode工程目录下面新建一个名为podfile的文本文件

    platform :ios, '9.0'
    
    target 'HealthTrainBusiness' do
      use_frameworks!
      # Pods for HealthTrainBusiness
      pod 'Alamofire', '~> 4.2'
    end
    
    

    平台为ios,最低9.0,以framework的形式引入,target后是你的工程名,在doend之间以每行的格式写明要引入的框架
    格式为pod '包名', '比较符号 版本号'

    另外在工程目录中可以使用pod init命令来自动生成一个Podfile

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

    第一行注释中写取消注释会在工程中定义一个全局的platform,一般情况是要这么做,如果不使用swiftdynamic frameworks,需要把use_frameworks!注释掉

    执行pod install

    Analyzing dependencies
    Downloading dependencies
    Installing Alamofire (4.2.0)
    Generating Pods project
    Integrating client project
    
    [!] Please close any current Xcode sessions and use `HealthTrainBusiness.xcworkspace` for this project from now on.
    Sending stats
    Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
    

    让我们以后都使用HealthTrainBusiness.xcworkspace

    项目结构

    引入Alamofire就可以用了
    import Alamofire

    如果你在中国

    由于gem的软件源被墙,所以你需要

    gem sources -l
    

    假如输出

    https://rubygems.org/
    

    请执行以下命令

    gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/
    

    把软件源换成国内的ruby-china的镜像,在按照开始的步骤安装CocoaPods

    相关文章

      网友评论

          本文标题:CocoaPods概述

          本文链接:https://www.haomeiwen.com/subject/axdsbttx.html