iOS开发日常-CocoaPods秘籍之【安装、使用】

基本概念

  CocoaPods manages library dependancies for your Xcode projects.
  The dependencies for your projects are specified in a single text file called a Podfile. CocoaPods will resolve dependencies between libraries, fetch the resulting source code, then link it together in an Xcode workspace to build your project.
  Ultimately, the goal is to improve discoverability of, and engagement in, third party open-source libraries by creating a more centralised ecosystem.

CocoaPods安装

  CocoaPods is built with Ruby and it will be installable with the default Ruby available on macOS. You can use a Ruby Version manager, however we recommend that you use the standard Ruby available on macOS unless you know what you’re doing.
  Using the default Ruby install will require you to use sudo when installing gems.

$ sudo gem install cocoapods

CocoaPods更新

To update CocoaPods you simply install the gem again.

$ sudo gem install cocoapods

Later on, when you’re actively using CocoaPods by installing pods, you will be notified when new versions become available with a CocoaPods X.X.X is now available, please update message.

CocoaPods使用

To create a new project with CocoaPods, follow these simple steps:

  • Create a new project in Xcode as you would normally.
  • Open a terminal window, and $ cd into your project directory.
  • Create a Podfile. This can be done by running $ pod init.
  • Open your Podfile. The first line should specify the platform and version supported.
    platform :ios, '9.0'
    
  • In order to use CocoaPods you need to define the Xcode target to link them to. So for example if you are writing an iOS app, it would be the name of your app. Create a target section by writing target ‘$TARGET_NAME’ do and an end a few lines after.
  • Add a CocoaPod by specifying pod ‘$PODNAME’ on a single line inside your target block.
     target 'MyApp' do
       pod 'ObjectiveSugar'
    end
    
  • Save your Podfile.
  • Run $ pod install.
  • Open the MyApp.xcworkspace that was created. This should be the file you use everyday to create your app.

几点说明:Podfile、Podfile.lock、pod install vs. pod update

  • Podfile

      The Podfile is a specification that describes the dependencies of the targets of one or more Xcode projects. The file should simply be named Podfile.
      A Podfile can be very simple (and can be more complex, of course), this adds Alamofire to a single target:

    target 'MyApp' do
       use_frameworks!
       pod 'Alamofire'
    end
    
  • Specifying pod versions

    When starting out with a project it is likely that you will want to use the latest version of a Pod. If this is the case, simply omit the version requirements.

    扫描二维码关注公众号,回复: 5324787 查看本文章
    pod 'Alamofire'
    

    Later on in the project you may want to freeze to a specific version of a Pod, in which case you can specify that version number.

    pod 'Alamofire', '0.9'
    

  • Podfile.lock

    This file is generated after the first run of pod install, and tracks the version of each Pod that was installed. For example, imagine the following dependency specified in the Podfile:

    pod 'TestKit'
    

    Running pod install will install the current version of TestKit, causing a Podfile.lock to be generated that indicates the exact version installed (e.g. TestKit 0.1.2). Thanks to the Podfile.lock, running pod install on this hypothetical project at a later point in time on a different machine will still install TestKit 0.1.2 even if a newer version is available. CocoaPods will honour the Pod version in Podfile.lock unless the dependency is updated in the Podfile or pod update is called (which will cause a new Podfile.lock to be generated). In this way CocoaPods avoids headaches caused by unexpected changes to dependencies.

  • pod install

      When you run pod install, it only resolves dependencies for pods that are not already listed in the Podfile.lock.
      For pods listed in the Podfile.lock, it downloads the explicit version listed in the Podfile.lock without trying to check if a newer version is available
      For pods not listed in the Podfile.lock yet, it searches for the version that matches what is described in the Podfile (like in pod ‘MyPod’, ‘~>1.2’).

  • pod update

      When you run pod update PODNAME, CocoaPods will try to find an updated version of the pod PODNAME, without taking into account the version listed in Podfile.lock. It will update the pod to the latest version possible (as long as it matches the version restrictions in your Podfile).
      If you run pod update with no pod name, CocoaPods will update every pod listed in your Podfile to the latest version possible.

  • pod outdated

    When you run pod outdated, CocoaPods will list all pods which have newer versions than the ones listed in the Podfile.lock (the versions currently installed for each pod). This means that if you run pod update PODNAME on those pods, they will be updated — as long as the new version still matches the restrictions like pod ‘MyPod’, ‘~>x.y’ set in your Podfile.

CocoaPods原理

猜你喜欢

转载自blog.csdn.net/FTD_SL/article/details/85766565