<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:
- 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 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.
@H_301_156@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 |