In the previous post we created a dynamic framework.
Now we’re going to share this code with the world, by turning it into a CocoaPod and uploading it to cocoapods.org:
First, drag your whole project folder to Terminal, and create a podspec. In Terminal, type:
pod lib create ExampleProjectName
(Replace ‘ExampleProjectName’ with the name of your project, but be sure to give it a different name to your framework project name)
This pod lib create command is like a pod wizard, and will go through a series of questions to customize your pod. Your answers should be Swift, and Yes to include a demo application. The wizard will generate a podspec file and an Xcode project and workspace that you can use to demonstrate how to use your library, and open it up automatically for you. If you want to look for the new Xcode workspace in the Finder, you’ll find it located in ExampleProjectName/Example/ExampleProjectName.xcworkspace.
In the Project Navigator in Xcode, find Podspec Metadata. In here are three files you’ll want to edit:
- PExampleProjectName.podspec – This specifies all the details of your CocoaPod, including name, version number, dependencies, minimum requirements etc. The wizard prepulates this file with prefilled details and comments (anything after #) to help you understsand the expectations. Go through this file editing it for your project.
- README.md – The README is prepopulated with some explanations of your pod, instructions how to install etc. Feel free to add additional details.
- LICENSE – A license for distribution of your code, This defaults to the MIT License, but feel free to edit this.
Now to edit your two projects.
- Remove the references of your Swift classes in your dynamic framework project(leave behind ProjectName.h and Info.plist)
- Move these classes from your project folder to your new example project, in the Pod/Classes folder.
- Drag references to the new locations of your classes back into where they were in the original project in Xcode. (deselect Copy items if needed)
- Build your dynamic framework to be sure all is still working properly.
- Drag references to the classes into your example project, in the Classes folder.
- Remove the ReplaceMe.swift file – you can Move to Trash.
- Now to install the pod – drag the Examples folder to the Terminal, and install your pod to your example project:
- Build your project.
- Hooray. You should be able to use your code in the example project! Write some code to demonstrate its use.
Validate your podspec
Now it’s time to check your CocoaPod passes validation. Enter the following into Terminal:
pod spec lint ProjectName.podspec
It should provide you with helpful guidance as to what changes you need to make, if any, to your podspec and files.
If you see the message:
ProjectName passed validation.
you’re good to go! Just one more thing to do.
Update the git tag
Before you upload to CocoaPods, you need to demonstrate that you are describing the same version of the github project in the podspec. To do this you need to give your github project a tag and push up to github.
git tag -a 0.01 -m "First CocoaPod version" git push --tags
Be sure that your version number matches the s.version number in your podspec.
Upload to CocoaPods
Now finally, we’re ready to upload to CocoaPods!
pod trunk push ProjectName.podspec
Soon you should see your project appear on CocoaPods! You can see my Sprite Kit Easing framework on CocoaPods here.
Install your pod
You should test your pod from a user’s perspective. Create a new Xcode project, and then create a Podfile. Mine looks like:
source 'https://github.com/CocoaPods/Specs.git' use_frameworks! platform :ios, '8.0' pod 'SpriteKitEasingSwift'
Install your CocoaPod:
You should see your pod install, and a workspace created. Open the workspace and build your project.
You should now be able to access your code. Don’t forget to import your framework:
Have to make changes to your code? Easy. After making your changes you just need to:
- Update version number in the podspec.
- Update the git tag and push i (as per above).
- Check everything is still valid (as per above).
- All good? Upload it to CocoaPods (you guessed it, as per above)