微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

swift 创建cocoapod

<a target=_blank href="http://www.raywenderlich.com/99386/create-cocoapod-swift">点击打开链接</a><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 10px; border: 0px; font-family: 'Open Sans',sans-serif; font-size: 16px; outline: 0px; vertical-align: baseline; line-height: 25.8799991607666px;">You’re probably familiar with some well-kNown,<a target=_blank target="_blank" href="https://github.com/CocoaPods/Specs/tree/master/Specs" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,104,55);">open-source CocoaPods</a>such as<a target=_blank target="_blank" href="https://github.com/Alamofire/Alamofire" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);">Alamofire</a>or<a target=_blank target="_blank" href="https://github.com/jdg/MBProgressHUD" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);">MBProgressHUD</a>. But sometimes you can’t find a a pod with the exact functionality you need,or you may want to separate a large project into smaller,reusable components.</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 10px; border: 0px; font-family: 'Open Sans',sans-serif; font-size: 16px; outline: 0px; vertical-align: baseline; line-height: 25.8799991607666px;">Fortunately,it’s easy to create your own CocoaPods!</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 10px; border: 0px; font-family: 'Open Sans',sans-serif; font-size: 16px; outline: 0px; vertical-align: baseline; line-height: 25.8799991607666px;">If you’ve already created a Cocoa Touch framework for your component,you’ve already done most of the hard work. If you haven’t,don’t panic as it’s really straightforward.</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 10px; border: 0px; font-family: 'Open Sans',sans-serif; font-size: 16px; outline: 0px; vertical-align: baseline; line-height: 25.8799991607666px;">If you’ve only ever created classes as part of an iOS app,that’s okay too. You can easily create a new pod by pulling out classes and functionality that make sense for your particular use case.</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 10px; border: 0px; font-family: 'Open Sans',sans-serif; font-size: 16px; outline: 0px; vertical-align: baseline; line-height: 25.8799991607666px;">This tutorial picks up where<a target=_blank target="_blank" href="http://www.raywenderlich.com/97014" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);">How to Use CocoaPods with Swift</a>ends. If you’ve never used CocoaPods before,then that tutorial is definitely a prerequisite to this one.</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 10px; border: 0px; font-family: 'Open Sans',sans-serif; font-size: 16px; outline: 0px; vertical-align: baseline; line-height: 25.8799991607666px;">Otherwise,grab yourself a hot cup of “cocoa” (<em>sorry,Couldn’t resist!</em>) and read on! :]</p><h2 style="border: 0px; font-family: Bitter,serif; font-size: 26px; margin: -3px 0px 0px; outline: 0px; padding: 5px 0px 20px; vertical-align: baseline; color: rgb(0,55); line-height: 25.8799991607666px;">Getting Started</h2><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 10px; border: 0px; font-family: 'Open Sans',sans-serif; font-size: 16px; outline: 0px; vertical-align: baseline; line-height: 25.8799991607666px;">Your top client is Ice Cream Shop,Inc. Their ice cream is so popular they can’t keep up with customer orders at the counter. They’ve recruited you to create a sleek iOS app that will allow customers to order ice cream right from their phone. You’ve started developing the app,and it’s coming along well.</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 10px; border: 0px; font-family: 'Open Sans',sans-serif; font-size: 16px; outline: 0px; vertical-align: baseline; line-height: 25.8799991607666px;">Download the starter project from<a target=_blank target="_blank" href="http://cdn2.raywenderlich.com/wp-content/uploads/2015/06/IceCreamShop_Final1.zip" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);">here</a>— this is the final project from<a target=_blank target="_blank" href="http://www.raywenderlich.com/97014" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);">How to Use CocoaPods with Swift</a>.</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 10px; border: 0px; font-family: 'Open Sans',sans-serif; font-size: 16px; outline: 0px; vertical-align: baseline; line-height: 25.8799991607666px;">The app has a few pod dependencies that are already included in the download,so you<em>don’t</em>need to run<span style="border: 0px; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);"><strong>pod install</strong></span>to install them.</p><div class="note" style="border: 1px solid black; font-family: 'Open Sans',sans-serif; font-size: 16px; margin: 5px 0px 17px; outline: 0px; vertical-align: baseline; line-height: 25.875px; padding: 14px !important; background-color: rgb(237,240,213);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; border: 0px; font-family: inherit; font-size: 1em; font-style: inherit; font-weight: inherit; outline: 0px; vertical-align: baseline;"><span style="border: 0px; font-family: Helvetica,sans-serif; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);"><strong>Note:</strong></span>If you worked through<a target=_blank target="_blank" href="http://www.raywenderlich.com/97014" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);">How to Use CocoaPods with Swift</a>this next section may seem familiar – it’s simply a review of that tutorial. Feel free to skip ahead as needed.</p></div><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 10px; border: 0px; font-family: 'Open Sans',sans-serif; font-size: 16px; outline: 0px; vertical-align: baseline; line-height: 25.8799991607666px;">Open<span style="border: 0px; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);"><strong>IceCreamShop.xcworkspace</strong></span>and then<span style="border: 0px; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);"><strong>Main.storyboard</strong></span>,found in the<span style="border: 0px; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);"><strong>Views\Storyboards & Nibs</strong></span>group,to see how the app is laid out.</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 10px; border: 0px; font-family: 'Open Sans',sans-serif; font-size: 16px; outline: 0px; vertical-align: baseline; line-height: 25.8799991607666px;">Here’s a quick overview of the<span style="border: 0px; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);"><strong>Choose Your Flavor</strong></span>scene,which is the heart of the application:</p><ul style="border: 0px; font-family: 'Open Sans',sans-serif; font-size: 16px; margin: 0px 0px 3px 30px; outline: 0px; padding: 0px; vertical-align: baseline; line-height: 25.8799991607666px;"><li style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px 0px 10px; outline: 0px; padding: 0px; vertical-align: baseline;"><span style="border: 0px; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);"><strong>PickFlavorViewController</strong></span>: handles user interaction,such as when the user selects an ice cream flavor.</li><li style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px 0px 10px; outline: 0px; padding: 0px; vertical-align: baseline;"><span style="border: 0px; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);"><strong>PickFlavorDataSource</strong></span>: is the data source for the collection view that displays ice cream flavors.</li><li style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px 0px 10px; outline: 0px; padding: 0px; vertical-align: baseline;"><span style="border: 0px; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);"><strong>IceCreamView</strong></span>: is a custom view that displays an ice cream cone and is backed by a<code style="border: 0px; font-family: 'Droid Sans Mono',sans-serif; font-size: 14px; font-style: inherit; font-weight: bold; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);">Flavor</code>model.</li><li style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px 0px 10px; outline: 0px; padding: 0px; vertical-align: baseline;"><span style="border: 0px; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);"><strong>ScoopCell</strong></span>: is a custom collection view cell that contains a<span style="border: 0px; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);"><strong>ScoopView</strong></span>,which is also backed by an instance of the<code style="border: 0px; font-family: 'Droid Sans Mono',55);">Flavor</code>model class.</li></ul><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 10px; border: 0px; font-family: 'Open Sans',sans-serif; font-size: 16px; outline: 0px; vertical-align: baseline; line-height: 25.8799991607666px;"><a target=_blank target="_blank" href="http://cdn2.raywenderlich.com/wp-content/uploads/2015/03/storyboard_annotated.png" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);"><img src="http://cdn3.raywenderlich.com/wp-content/uploads/2015/03/storyboard_annotated-427x500.png" alt="Storyboard Annotated" title="Storyboard Annotated" width="427" height="500" class="aligncenter size-large wp-image-98978" style="max-width: 650px; height: auto; vertical-align: top; border: 0px; clear: both; display: block; margin-left: auto; margin-right: auto; margin-bottom: 1.625em;" /></a></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 10px; border: 0px; font-family: 'Open Sans',sans-serif; font-size: 16px; outline: 0px; vertical-align: baseline; line-height: 25.8799991607666px;">The top managers at Ice Cream Shop really like how the app is coming along,but they’ve added a new requirement: ice cream retailers need to have the same choose-your-own-flavor functionality in their apps.</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 10px; border: 0px; font-family: 'Open Sans',sans-serif; font-size: 16px; outline: 0px; vertical-align: baseline; line-height: 25.8799991607666px;">Wait,that wasn’t in the original design. But that should be no probelm for a super developer like you!</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 10px; border: 0px; font-family: 'Open Sans',sans-serif; font-size: 16px; outline: 0px; vertical-align: baseline; line-height: 25.8799991607666px;"><a target=_blank target="_blank" href="http://cdn1.raywenderlich.com/wp-content/uploads/2014/11/SuperDev.jpg" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);"><img src="http://cdn1.raywenderlich.com/wp-content/uploads/2014/11/SuperDev.jpg" alt="SuperDev" width="303" height="194" class="aligncenter size-full wp-image-88162" style="max-width: 650px; height: auto; vertical-align: top; border: 0px; clear: both; display: block; margin-left: auto; margin-right: auto; margin-bottom: 1.625em;" /></a></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 10px; border: 0px; font-family: 'Open Sans',sans-serif; font-size: 16px; outline: 0px; vertical-align: baseline; line-height: 25.8799991607666px;">Can you guess how you’re going to do this? Yep,you’re going to pull this functionality into its own CocoaPod!</p><h2 style="border: 0px; font-family: Bitter,55); line-height: 25.8799991607666px;">Setting up Your Pod</h2><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 10px; border: 0px; font-family: 'Open Sans',sans-serif; font-size: 16px; outline: 0px; vertical-align: baseline; line-height: 25.8799991607666px;">Create a new Xcode project and select<span style="border: 0px; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);"><strong>iOS\Framework & Library\Cocoa Touch Framework</strong></span>,and then click<span style="border: 0px; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);"><strong>Next</strong></span>:</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 10px; border: 0px; font-family: 'Open Sans',sans-serif; font-size: 16px; outline: 0px; vertical-align: baseline; line-height: 25.8799991607666px;"><a target=_blank target="_blank" href="http://cdn3.raywenderlich.com/wp-content/uploads/2015/03/cocoa_touch_framework.png" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);"><img src="http://cdn4.raywenderlich.com/wp-content/uploads/2015/03/cocoa_touch_framework-480x284.png" alt="Create Cocoa Touch Framework" title="Create Cocoa Touch Framework" width="480" height="284" class="aligncenter size-medium wp-image-100395" style="max-width: 650px; height: auto; vertical-align: top; border: 0px; clear: both; display: block; margin-left: auto; margin-right: auto; margin-bottom: 1.625em;" /></a></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 10px; border: 0px; font-family: 'Open Sans',sans-serif; font-size: 16px; outline: 0px; vertical-align: baseline; line-height: 25.8799991607666px;">Enter<span style="border: 0px; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);"><strong>RWPickFlavor</strong></span>for the product name and<span style="border: 0px; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);"><strong>Swift</strong></span>for the language. Click<span style="border: 0px; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);"><strong>Next</strong></span>.</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 10px; border: 0px; font-family: 'Open Sans',sans-serif; font-size: 16px; outline: 0px; vertical-align: baseline; line-height: 25.8799991607666px;">For the purposes of this tutorial you will create the project in<span style="border: 0px; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);"><strong>~/Documents/Libraries</strong></span>. Choose the<span style="border: 0px; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);"><strong>Documents</strong></span>folder in your home directory. If you don’t already have a<span style="border: 0px; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);"><strong>Libraries</strong></span>folder,click on the<span style="border: 0px; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);"><strong>New Folder</strong></span>button in the bottom and create it.</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 10px; border: 0px; font-family: 'Open Sans',sans-serif; font-size: 16px; outline: 0px; vertical-align: baseline; line-height: 25.8799991607666px;">Finally,choose the<span style="border: 0px; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);"><strong>Libraries</strong></span>folder and click<span style="border: 0px; font-family: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(0,55);"><strong>Create</strong></span>.</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 10px; border: 0px; font-family: 'Open Sans',sans-serif; font-size: 16px; outline: 0px; vertical-align: baseline; line-height: 25.8799991607666px;">The directory where you save your development pods is important because you will need to reference this directory from your Podfile during local development.</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 10px; border: 0px; font-family: 'Open Sans',sans-serif; font-size: 16px; outline: 0px; vertical-align: baseline; line-height: 25.8799991607666px;">normally,when you<em>use</em>CocoaPods,you include dependencies in your Podfile like this:</p><div class="wp_codeBox" style="border: 1px solid silver; font-family: 'Open Sans',sans-serif; font-size: 16px; margin: 0px 0px 0.5em; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(17,0); width: 782.03125px; overflow: auto; line-height: 25.8799991607666px; background-color: rgb(249,249,249);"><table class=" " style="border: 0px; font-family: inherit; font-size: 16px; font-style: inherit; font-weight: inherit; outline: 0px; padding: 0px; vertical-align: baseline; border-collapse: collapse; border-spacing: 0px; width: 782px; margin: 0px !important;"><tbody style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><tr id="p993861" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><td class="code" id="p99386code1" style="font-family: inherit; font-size: 16px; border: 0px; font-style: inherit; margin: 0px; outline: 0px; padding: 2px 4px; vertical-align: top;"><pre class="ruby" style="margin-top: 0px; margin-bottom: 0px; border: 0px; font-size: 12px; font-style: inherit; font-weight: inherit; outline: 0px; padding: 0px; vertical-align: baseline; width: auto; float: none; clear: none; overflow: visible; line-height: 1.333; word-wrap: break-word; white-space: pre-wrap; background: 0px 50%;">pod <span style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(153,102,0);">'PodName'</span>,<span style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(153,0);">'~> 1.0'</span>

But when you’re developingyour ownCocoaPod,you will instead specify a local path,like this:

pod 'MyPodName',:path => '~/Path/To/Folder/Containing/My/Pod'

There are two benefits to this approach:

  1. It uses the local files on your machine for the pod,instead of fetching them from a remote repository.
  2. normally,you don’t make changes to a pod included by your app because those changes would be overwritten the next time you runpod install,as the pod will be re-fetched from the remote repository and the source files you changed would be overwritten. By using the:path =>Syntax,you can easily make changes to the pod during development that won’t be overwritten,since the specified path is Now thesourceof the CocoaPod,and thus your changes won’t be lost when you runpod installagain.

While you can use a different location for your development pods,in general I recommend putting them in~/Documents/Libraries. This is also a good location if you work with a team of developers since CocoaPods kNows to expand the tilde as the user’s home directory; therefore you don’t need to hard code an absolute path in the Podfile to this directory.

You can also use other CocoaPods within your project as dependencies for the CocoaPod you created — you simply need a Podfile to manage your CocoaPod’s dependencies.

Close Xcode,then enter the following commands in Terminal:

cd ~/Documents/Libraries/RWPickFlavor
pod init
open -a Xcode Podfile

This creates a new Podfile and opens it in Xcode.

Replace the entire contents of the new Podfile with the following:

platform :ios,'8.0'
use_frameworks!

target 'RWPickFlavor' do
  pod 'Alamofire',0)">'~> 1.2'
  pod 'MBProgressHUD',0)">'~> 0.9.0'
end

This declares thatRWPickFlavorwill have external dependencies on bothAlamofireandMBProgressHUD.

Save and close the Podfile,then enter the following command in Terminal:

pod install

Just as you’d expect,this creates a workspace and installs the varIoUs requisite files.

Note: Ifpod installgives you any warnings,then you’re likely using an older version of CocoaPods. Swift based CocoaPods,such as Alamofire,require CocoaPods version 0.36.0 or newer. You can check the version of CocoaPods you have installed by running the following command in Terminal:

pod --version

If that’s the case,enter the following in Terminal to install the latest version of CocoaPods:

sudo gem install CocoaPods

Enter the following command in Terminal to open the newly createdRWPickFlavorworkspace:

open RWPickflavor.xcworkspace

Your project navigator should Now look like the following:

You Now need to copy a few of the existing files from theIceCreamShopworkspace intoRWPickFlavor.

First,create the following groups inRWPickFlavor.xcworkspaceto hold the files you’re going to copy:

  • Categories
  • Controllers
  • Factories
  • Models
  • Views
    • Ice Cream
    • Storyboards & Nibs

Now drag and drop everything —exceptforAppDelegate.swiftandLaunchScreen.xib— from the above groups inIceCreamShop.xcworkspaceinto the corresponding groups inRWPickFlavor.xcworkspacelike so:

When prompted,ensure thatCopy items if neededis checked so that each file will actually be copied instead of simply linked.

When you’re done,RWPickFlavorshould have the following files:

  • RWPickFlavor
    • RWPickFlavor.h
      • Categories
        • RGBAColorFromString.swift
      • Controllers
        • PickFlavorDataSource.swift
        • PickFlavorViewController.swift
      • Factories
          FlavorFactory.swift
      • Models
          Flavor.swift
      • Supporting Files
          Info.plist
      • Views

      • Ice Cream
        • IceCreamView.swift
        • ScoopCell.swift
        • ScoopView.swift
      • Storyboard & Nibs
        • Main.storyboard

Once you’re sure all the files have been copied over,55)">deletethe originals and any empty groups fromIceCreamShop,leaving just the files inRWPickFlavor. Take care not to delete any of the following:

  • AppDelegate.swift
  • LaunchScreen.xib
  • Images.xcassets
  • Anything under theSupporting Filesgroup

Now openInfo.plist,found under theSupporting Filesgroup,and delete the line forMain storyboard file base name.

Build and run. You shouldn’t see any errors,and you’ll eventually see the “Ice Cream Shop” logo followed by a black screen.

What About the Images?

You may have noticed that you didn’t copy over theResourcesgroup; this is because you need to copy justbackground.jpgitself into theResourcesfolder ofRWPickFlavor— not the completeImages.xcassetsfile.

Resourcesgroup within Next,selectImages.xcassetsinbackgroundand selectShow In Finder,like so:

Now drag and dropbackground.jpgfromFinderinto theResourcesgroup inRWPickFlavor. Again,make sure that you checkCopy items if neededwhen prompted. After you’ve copied the image,delete the originalbackgroundfromIceCreamShop.

Finally,inRWPickFlavorupdate the image view’s image on theChoose Your Flavorscene inMain.storyboardso that it refers tobackground.jpginstead of simplybackground:

Believe it or not,the hardest part of creating your pod is done! :]

CocoaPods and Git

Since CocoaPods is bootstrapped on top of Git,each pod will need to have its own Git repository. If you already have a preferred Git host,great — you can use it to host your repository.

If you don’t,GitHubis an excellent choice as it’s well-kNown by developers and has a free plan for open-source projects.

Bitbucketis another great option as it has a free unlimited tier,including private repositories,for teams of up to five developers.

This tutorial usesGitHub,but feel free to use your preferred Git host instead.

GitHub Repo Setup

ottom:0px; padding-top:0px; padding-bottom:10px; border:0px; font-family:'Open Sans',Sign uporLoginto your GitHub account.

ottom:0px; padding-top:0px; padding-bottom:10px; border:0px; font-family:'Open Sans',click on the+(create new) icon on the top right of the screen and selectNew repositoryas shown below:
EnterRWPickFlavorfor theRepository name,and selectCreate repository.

Github will create a new repository under your account; you’ll then see the following screen with aQuick setupsection that displays your repository URL:

You’ll need this URL in just a moment,so leave the page open for now.

Now you need a second repository to host all of your private pod specs — you’ll use this later on in the tutorial.

Opengithub.comin a new tab; again,press theCreate newicon and selectNew repository. Name this repositoryRWPodSpecs,sans-serif; font-size:16px; outline:0px; vertical-align:baseline; line-height:25.8799991607666px"> Leave this tab open as well so you can easily grab the URL later when you need it.

Podspec Setup

Now you need to create theRWPickFlavor.podspecfile forRWPickFlavor. ThePodspecincludes basic information such as the pod’s name,version and Git download URL.

Enter the following commands in Terminal,hitting enter after each one:

cd ~/Documents/Libraries/RWPickFlavor
pod spec create RWPickFlavor
open -a Xcode RWPickFlavor.podspec

This createsRWPickFlavor.podspecand opens it in Xcode.

There’s a lot of excellent documentation and examples in the default Podspec – however,you don’t need most of it.

Replace everything inRWPickFlavor.podspecwith the following:

Pod::Spec.new do |s|

  # 1
  s.platform = :ios
  s.ios.deployment_target = '8.0'
  s.name = "RWPickFlavor"
  s.summary = "RWPickFlavor lets a user select an ice cream flavor."
  s.requires_arc = true

  # 2
  s.version = "0.1.0"

  # 3
  s.license = { :type "MIT",51)">:file "LICENSE" }

  # 4 - Replace with your name and e-mail address
  s.author = { "[Your Name Goes Here]" "[Your_Email@Your_Email_Domain.com]" # For example,
  # s.author = { "Joshua Greene" => "jrg.developer@gmail.com" }


  # 5 - Replace this URL with your own Github page's URL (from the address bar)
  s.homepage = "[Your RWPickFlavor Homepage URL Goes Here]"

  # s.homepage = "https://github.com/JRG-Developer/RWPickFlavor"


  # 6 - Replace this URL with your own Git URL from "Quick Setup"
  s.source = :git "[Your RWPickFlavor Git URL Goes Here]",51)">:tag "#{s.version}"# s.source = { :git => "https://github.com/JRG-Developer/RWPickFlavor.git",:tag => "#{s.version}"}


  # 7
  s.framework = "UIKit"
  s.dependency '~> 1.1'
  s.'~> 0.9.0'

  # 8
  s.source_files = "RWPickFlavor/**/*.{swift}"

  # 9
  s.resources = "RWPickFlavor/**/*.{png,jpeg,jpg,storyboard,xib}"
 Just like a Podfile,the Podspec is written in Ruby. Be extra careful not to make any typos,or else the pod will likely fail to validate and/or install later.

Here’s what’s going on:

  1. You first specify basic information about the pod. Swift based CocoaPodsmusthave a minimum deployment target of 8.0 or higher. If you specify a lower version,the pod won’t install correctly.
  2. A Podspec is essentially a snapshot in time of your CocoaPod as denoted by a version number. When you update a pod,you will also need to update the Podspec’s version. All CocoaPods are highly encouraged to followSemantic Versioning. If you’re not familiar with Semantic Versioning,seeHow to Use CocoaPods with Swiftfor more information.
  3. All pods must specify a license. If you don’t,CocoaPods will present a warning when you try to install the pod,and you won’t be able to upload it to CocoaPodstrunk– the master specs repo.
  4. Here you specify information about yourself,the pod author. Enter your name and e-mail address instead of the placeholder text.
  5. Here you need to specify the URL for your pod’s homepage. It’s okay to simply copy and paste the Github homepage from your browser’s address bar to use here.
  6. Replace this URL with the Git download URL from the “Quick Setup” section of the first repo you created above. In general,it’s best to use either ahttp:orhttps:URL to make it easier for other users to consume. You can use an SSH URL if you want,but you’ll need to make sure that everyone on your team — and whoever else needs access to the CocoaPod — already has their public/private key pairs setup with your Git host.
  7. Here you specify the framework and any pod dependencies.
  8. Here you specify thepublic source filesbased on file extensions; in this case,you specify.swiftas the extension.
  9. Finally,you specify theresourcesbased on their file extensions.

Like every other pod,you’ll need to create theLICENSEfile.

copy and paste the MIT License foundhereinto your favorite text editor,and then save the file asLICENSE– with no extension – in~/Documents/Libraries/RWPickFlavor. Make sure to replace[year]and[fullname]with the actual values- uh,the year and your actual name,of course. ;]

Choose a Licenseis a really great site that helps you choose the most appropriate open-source license for your project,and is built and maintained by the good folks at GitHub.

Push to Git

You’re finally ready to push theRWPickFlavorpod to its new home on GitHub!

ottom:0px; padding-top:0px; padding-bottom:10px; border:0px; font-family:'Open Sans',replacing[Your RWPickFlavor Git URL]with the Git URL forRWPickFlavorrepo you created earlier:

cd ~/Documents/Libraries/RWPickFlavor
git init
git add .
git commit -m "Initial commit"
git tag 0.1.0
git remote add origin [Your RWPickFlavor Git URL]
git push -u origin master --tags

If prompted,enter your username and password for Github.

This commits all of the files within theRWPickFlavordirectory,creates a new0.1.0tag,and pushes eveything to your remote repository.

Congratulations,you’ve just created your first CocoaPod!

You’ve created your first CocoaPod,but can you actually use it? Well,not quite yet. :]

You first need to add your Podspec to a private specs repo; this lets CocoaPods find the pod when you try to install it. Fortunately,you’ve already created a Git repo for this,so this final step is relatively straightforward.

Enter the following in Terminal,making sure you’re still in theRWPickFlavordirectory:

pod repo add RWPodSpecs [Your RWPodSpecs Git URL]
pod repo push RWPodSpecs RWPickFlavor.podspec

Make sure to replace[Your RWPodSpecs Git URL]with the Git URL forRWPodSpecsrepo you created earlier.

This creates a local reference toRWPodSpecsthat’s stored in~/.cocoapodson your machine,and pushes theRWPickFlavor.podspecto it.

You now have a private pod specs repo set up! Easier than you thought,right?

Using Your New CocoaPod

It’s finally time to use your newly created pod.

Open the Podfile forIceCreamShopand replace its contents with the following:

platform '8.0'

source 'https://github.com/CocoaPods/Specs.git'
source '[Your RWPodSpecs Git URL Goes Here]'

use_frameworks!

target 'IceCreamShop' 'RWPickFlavor',0)">'~/Documents/Libraries/RWPickFlavor'
 Make sure that you replace[Your RWPodSpecs Git URL Goes Here]with the Git URL for yourRWPodSpecsrepo.

Then,runpod installin Terminal.

AppDelegate.swiftwith the following:

import UIKit
import RWPickFlavor

@UIApplicationMain
class AppDelegate: UIResponder,UIApplicationDelegate {

  var windowUIWindow?
  var rootViewControllerUIViewController!

  func application(applicationUIApplication,didFinishLaunchingWithOptions 
    launchOptions: [NSObject : AnyObject]?) -> Bool {

    setupRootViewController()

    window = UIWindow(frame: UIScreen.mainScreen).bounds)
    window?.rootViewController = rootViewController
    window?.makeKeyAndVisible)

    return true
  }

  func setupRootViewController{
    let bundle NSBundle(forClass: PickFlavorViewController.self)
    let storyboard = UIStoryboard(name: "Main",bundle: bundle)
    rootViewController = storyboard.instantiateInitialViewController) as! UIViewController
  }
}

InsetupRootViewController(),you get a reference to theRWPickFlavor“bundle” – which is actually a dynamic framework – create a reference to theMain.storyboard,and instantiate its root view controller.

Build and run. You’ll be greeted with the familiar “Choose Your Flavor” screen. Awesome! :]

Abstracting All The Things!

If you’re like me,you’re probably thinking,“Wow,App Delegate sure knows a lot about the structure of RWPickFlavor!”

Fortunately,there’s something you can do to reduce this tight-coupling: useBetterBaseClasses– a CocoaPod that makes creating other CocoaPods easier.

Add the following code to the pod file forRWPickFlavor,right after the line forAlamofire:

pod 'BetterBaseClasses',0)">'~> 1.0'

Likewise,add the following toRWPickFlavor.podspec,right below the line for

s. Now replace thes.versionline with the following:

s."0.2.0"

Here you’re declaringBetterBaseClassesas a dependency,and then bumping the version of your CocoaPod.

Now runpod installin Terminal to install this new dependency.

ottom:0px; padding-top:0px; padding-bottom:10px; border:0px; font-family:'Open Sans',add the following import toPickFlavorViewController,again right after the line for

import BetterBaseClasses

Then replace the class deFinition with the following:

public class PickFlavorViewController: BaseViewController,UICollectionViewDelegate {

This changesPickFlavorViewControllerso it inherits fromBaseViewController,which is part ofBetterBaseClasses.

Now you need to push these changes to both yourRWPickFlavorandRWPodSpecsrepositories. Run the following commands in Terminal:

"Added BetterBaseClasses dependency"
git tag 0.2.0
git push origin master --tags
pod repo push RWPodSpecs RWPickFlavor.podspec

ottom:0px; padding-top:0px; padding-bottom:10px; border:0px; font-family:'Open Sans',you need to pull this change into Update the Podfile forIceCreamShopby replacing thepod 'RWPickFlavor'line with the following:

pod '~> 0.2.0'

Here you’re updating the Podfile to request the version ofRWPickFlavoryou just pushed.

Then executepod installin Terminal to update this dependency in Lastly,replace the entire contents ofAppDelegate.swiftwith the following:

UIWindow?

  {

    window UINavigationController(rootViewController: 
      PickFlavorViewController.instanceFromStoryboard))
    window?.makeKeyAndVisible That’s much simpler!

BetterBaseClassesadds categories toUIViewController,UITableViewController,and other UIkit classes. This includes a category calledUIViewController+BetterBaseClasses,which adds convenience methods such asinstanceFromStoryboard()to make it super easy to instantiate view controllers,whether they live in the main bundle or somewhere else,such as a framework in this case.

Build and run. Once again,you should be greeted with the familiar “Choose Your Flavor” screen.

Where to Go From Here?

You can download the completed IceCreamShop projecthere,and the RWPickFlavor podhere.

You’re Now ready to start making your own CocoaPods! However,what you’ve covered in this tutorial is really just the tip of the iceberg when it comes to CocoaPods. Check out theCocoaPods Guidesto learn everything you need to kNow about creating CocoaPods.

After you create a CocoaPod,you might consider adding it to theCocoaPods Master Specs Reposo that it will be made available viaCocoaPods.orgto thousands of developers around the world. Refer to theCocoaPods Trunkblog post to find out how you can do this!

If you have any questions or comments about the tutorial,feel free to join the discussion in the comments below!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐