您的位置:首页 > 移动开发 > Swift

swift 创建cocoapod

2015-07-02 17:35 615 查看
<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, 104, 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, 104, 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, 104, 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, 104, 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, 104, 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, 104, 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, 104, 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, 104, 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, 104, 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, 104, 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, 104, 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, 104, 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, 104, 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, 104, 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, 104, 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, 104, 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, 104, 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, 104, 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, 104, 55);"><strong>ScoopView</strong></span>, which is also backed by an instance of the <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, 104, 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, 104, 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, 104, 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, serif; font-size: 26px; margin: -3px 0px 0px; outline: 0px; padding: 5px 0px 20px; vertical-align: baseline; color: rgb(0, 104, 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, 104, 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, 104, 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, 104, 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, 104, 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, 104, 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, 104, 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, 104, 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, 104, 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, 104, 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, 104, 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, 104, 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, 104, 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, 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, 102, 0);">'~> 1.0'</span>


But when you’re developing your own CocoaPod, 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:

It uses the local files on your machine for the pod, instead of fetching them from a remote repository.

Normally, you don’t make changes to a pod included by your app because those changes would be overwritten the next time you run pod
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 the source of the CocoaPod, and thus your changes won’t be lost when you run pod
install
again.

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', '~> 1.2'
pod 'MBProgressHUD', '~> 0.9.0'
end

This declares that RWPickFlavor will have external dependencies on both Alamofire and MBProgressHUD.

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: If pod
install
gives 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 created RWPickFlavor workspace:

open RWPickflavor.xcworkspace

Your project navigator should now look like the following:





You now need to copy a few of the existing files from the IceCreamShop workspace into RWPickFlavor.

First, create the following groups in RWPickFlavor.xcworkspace to hold the files you’re
going to copy:

Categories

Controllers

Factories

Models

Views

Ice Cream

Storyboards & Nibs

Now drag and drop everything — except for AppDelegate.swift and LaunchScreen.xib
from the above groups in IceCreamShop.xcworkspace into the corresponding groups in RWPickFlavor.xcworkspace like
so:





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

When you’re done, RWPickFlavor should 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, delete the originals and any
empty groups fromIceCreamShop, leaving just the files in RWPickFlavor.
Take care not to delete any of the following:

AppDelegate.swift

LaunchScreen.xib

Images.xcassets

Anything under the Supporting Files group

Now open Info.plist, found under the Supporting
Files
group, and delete the line for Main 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 the Resources group; this is because
you need to copy justbackground.jpg itself into the Resources folder
of
RWPickFlavor
— not the complete Images.xcassetsfile.

First, create a new Resources group within RWPickFlavor.

Next, select Images.xcassets in IceCreamShop,
right click on background and select Show
In Finder
, like so:





Now drag and drop background.jpg from Finder into
the Resources group in RWPickFlavor.
Again, make sure that you check Copy items if needed when prompted. After you’ve copied
the image, delete the original background from Images.xcassets in IceCreamShop.

Finally, in RWPickFlavor update the image view’s image on the Choose
Your Flavor
scene inMain.storyboard so that it refers to background.jpg instead
of simply background:





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, GitHub is an
excellent choice as it’s well-known by developers and has a free plan for open-source projects.

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

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


GitHub Repo Setup

First, Sign up or Login to
your GitHub account.

Next, click on the + (create new) icon on the top right of the screen and select New
repository
as shown below:





Enter RWPickFlavor for the Repository
name
, and select Create repository.

Github will create a new repository under your account; you’ll then see the following screen with a Quick
setup
section 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.

Open github.com in a new tab;
again, press the Create new icon and select New
repository
. Name this repository RWPodSpecs, and select Create
repository
.

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 the RWPickFlavor.podspec file for RWPickFlavor.
The Podspec includes 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 creates RWPickFlavor.podspec and 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 in RWPickFlavor.podspec with 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", :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]"

# For example,
# 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]", :tag => "#{s.version}"}

# For example,
# s.source = { :git => "https://github.com/JRG-Developer/RWPickFlavor.git", :tag => "#{s.version}"}

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

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

# 9
s.resources = "RWPickFlavor/**/*.{png,jpeg,jpg,storyboard,xib}"
end

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:

You first specify basic information about the pod. Swift based CocoaPods must have a minimum deployment target of 8.0 or higher. If you specify a lower version, the pod won’t install correctly.

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 follow Semantic
Versioning. If you’re not familiar with Semantic Versioning, see How
to Use CocoaPods with Swift for more information.

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 CocoaPods trunk
the master specs repo.

Here you specify information about yourself, the pod author. Enter your name and e-mail address instead of the placeholder text.

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.

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 a http: or https: 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.

Here you specify the framework and any pod dependencies.

Here you specify the public source files based on file extensions; in this case, you
specify
.swift
as the extension.

Finally, you specify the resources based on their file extensions.

Like every other pod, you’ll need to create the LICENSE file.

Copy and paste the MIT License found here into
your favorite text editor, and then save the file as LICENSE– 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 License is
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 the RWPickFlavor pod to its new home on GitHub!

Enter the following commands in Terminal, replacing [Your RWPickFlavor Git URL] with
the Git URL forRWPickFlavor repo 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 the RWPickFlavor directory, creates a new 0.1.0 tag,
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 the RWPickFlavor directory:

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 for RWPodSpecs repo
you created earlier.

This creates a local reference to RWPodSpecs that’s stored in ~/.cocoapods on
your machine, and pushes the RWPickFlavor.podspec to 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 for IceCreamShop and replace its contents with the following:

platform :ios, '8.0'

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

use_frameworks!

target 'IceCreamShop' do
pod 'RWPickFlavor', :path => '~/Documents/Libraries/RWPickFlavor'
end

Make sure that you replace [Your RWPodSpecs Git URL Goes Here] with the Git URL for
yourRWPodSpecs repo.

Then, run
pod install
in Terminal.

Finally, replace the entire contents of
AppDelegate.swift
with
the following:

import UIKit
import RWPickFlavor

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
var rootViewController: UIViewController!

func application(application: UIApplication, 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
}
}

In
setupRootViewController()
, you get a reference
to the
RWPickFlavor
“bundle” – which is actually
a dynamic framework – create a reference to the
Main.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: use BetterBaseClasses
a CocoaPod that makes creating other CocoaPods easier.

Add the following code to the pod file for RWPickFlavor, right after the line for Alamofire:

pod 'BetterBaseClasses', '~> 1.0'

Likewise, add the following to RWPickFlavor.podspec, right below the line for Alamofire:

s.dependency 'BetterBaseClasses', '~> 1.0'

Now replace the s.version line with the following:

s.version = "0.2.0"

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

Now run pod install in Terminal to install this new dependency.

Next, add the following import to
PickFlavorViewController
,
again right after the line for Alamofire:

import BetterBaseClasses

Then replace the class definition with the following:

public class PickFlavorViewController: BaseViewController, UICollectionViewDelegate {

This changes
PickFlavorViewController
so it inherits
from
BaseViewController
, which is part ofBetterBaseClasses.

Now you need to push these changes to both your RWPickFlavor and RWPodSpecs repositories.
Run the following commands in Terminal:

cd ~/Documents/Libraries/RWPickFlavor
git add .
git commit -m "Added BetterBaseClasses dependency"
git tag 0.2.0
git push origin master --tags
pod repo push RWPodSpecs RWPickFlavor.podspec

Next, you need to pull this change into IceCreamShop.

Update the Podfile for IceCreamShop by replacing the
pod
'RWPickFlavor'
line with the following:

pod 'RWPickFlavor', '~> 0.2.0'

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

Then execute
pod install
in Terminal to update
this dependency in IceCreamShop.

Lastly, replace the entire contents of AppDelegate.swift with the following:

import UIKit
import RWPickFlavor

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

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

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

return true
}
}

That’s much simpler!

BetterBaseClasses adds
categories to
UIViewController
,
UITableViewController
,
and other UIkit classes. This includes a category called
UIViewController+BetterBaseClasses
,
which adds convenience methods such as
instanceFromStoryboard()
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 project here,
and the RWPickFlavor pod here.

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 the CocoaPods
Guides to learn everything you need to know about creating CocoaPods.

After you create a CocoaPod, you might consider adding it to the CocoaPods
Master Specs Repo so that it will be made available via CocoaPods.org to
thousands of developers around the world. Refer to theCocoaPods
Trunk blog 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!


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: