Everyone who comes into contact with programming for iOS or macOS has certainly heard the term CocoaPods. It’s a so-called dependency manager, which allows you to add libraries to your project in a simple and pleasant way. It is so popular that almost every public library is available through the use of CocoaPods. We often create our own components, extensions that we use in many projects. Maintaining such physical files is very inefficient. In addition, people who use the same components often make corrections, which is why nobody knows who has the most current version of the code (which increases the risk of potential errors in the application). The solution may be to create your own Pod, which will introduce order and automation (after entering one line of code all your extensions added to the Pod will be included in your project).
Creating your own Pod – step by step:
-
Generating a template
To create a Pod template, CocoaPods has prepared a ready mechanism, which is activated with the following command:
1 |
pod lib create mySimplePod //example name |
After successful configuration, the project containing your Pod should open automatically.
-
.podspec configuration
Then, fill out the information about your module:
- Git configuration – first you have to think about the level of availability of your Pod to other programmers and determine whether your repository will be public or private. After creating the repository, fill in the addresses in the .podspec file and add your remote to the git created in the root directory
- Language version – Swift 4.1 has been selected in the system configuration by default, so add the
swift_version
attribute, and fill out thesummary
- This is what the contents of your .podspec file should look like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Pod::Spec.new do |s| s.name = 'mySimplePod' s.version = '0.1.0' s.summary = 'My first simple CocoaPod' s.swift_version = '4.1' s.description = <<-DESC "A long description of mySimplePod" DESC s.homepage = 'https://bitbucket.org/piotrlopata/mysimplepod' s.license = { :type => 'MIT', :file => 'LICENSE' } s.author = { 'pjjotr' => 'piotr.lopata@appchance.com' } s.source = { :git => 'https://bitbucket.org/piotrlopata/mysimplepod.git', :tag => s.version.to_s } s.ios.deployment_target = '8.0' s.source_files = 'mySimplePod/Classes/**/*' end |
- Validation of the .podspec file:
- To check whether you have configured everything correctly, enter the command
1pod lib lint
- To check whether you have configured everything correctly, enter the command
-
- If you want to make your module public, it cannot contain any warnings from this validator (Xcode warnings are OK)
- If you want to make your module public, it cannot contain any warnings from this validator (Xcode warnings are OK)
3. Extensions
The most important element of the process is adding your extensions to the project. Remember that the path to your source files in the .podspec file is set by default to the Classes
folder (files added elsewhere will not be available in your project after installing the Pod. The path can be edited in the .podspec file in the source_files field)
After adding your file, the folder structure in the project should look like this:
Then add your code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// // String+Extensions.swift // mySimplePod // // Created by Piotr Łopata on 25.06.2018. // import Foundation extension String { func firstCharacterCapitalized() -> String { return prefix(1).uppercased() + dropFirst() } } |
4. Creating a version of your Pod
Make sure that all your changes have been placed in your repository by entering the following commands:
1 2 3 |
git add . git commit -m 'Release 0.1.0' git push origin master |
Next, create a tag with the current version number of your module.
1 2 |
git tag '0.1.0' git push --tags |
Publishing the pod on CocoaPods.org (public repository only):
1 |
git trunk push mySimplePod.podspec |
If you have a problem with publishing the Pod, make sure that your session is configured correctly
1 |
pod trunk register piotr.lopata@appchance.com 'Piotr Łopata' --description='macbook pro' |
Your page with the Pod repository should look like this:
-
Pod installation
If you haven’t yet configured the Pods in the project, you can quickly do so by entering the command:
1 |
pod init |
while in the main folder containing your project file.
Sample content of Podfile file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
platform :ios, '10.3' use_frameworks! inhibit_all_warnings! def shared_pods //Create dependecy pod 'RxSwift' end target 'mySimpleProject' do shared_pods end post_install do |installer| installer.pods_project.targets.each do |target| target.build_configurations.each do |config| config.build_settings['SWIFT_VERSION'] = '4.0' end end end |
After configuration, you can add a line that will be responsible for adding your Pod to the project.
1 |
pod 'mySimplePod' |
If it is private, you need to add the address of your repository
1 |
pod 'mySimplePod', :git => 'https://bitbucket.org/piotrlopata/myspimplepod.git' |
Then in the terminal enter
1 |
pod install |
After successful installation, the Pod is available in your project 🙂
Remember that using CocoaPods, the project is initiated with the .xcworkspace file – if you use .xcodeproj, your project will not build!
Now just import your module:
1 |
import mySimplePod |
and you can use it:
1 2 3 |
let simpleString = "simple" let capitalizedString = simpleString.firstCharacterCapitalized() print("capitalized string: \(capitalizedString)") |
output:
1 |
capitalized string: Simple //output |
Finished! Your Pod is properly created and fully configured.
Environment:
CocoaPods – 1.4.0
Xcode 9.4.1
Swift 4.1
macOS 10.13.5
You might also like

Increasing mobile app retention focuses on the users returning…

Most marketing and e-commerce experts agree that m-commerce applications…

The number of people shopping through mobile devices is…

There has been much talk of e-commerce in recent…

App Store Optimization, or ASO, entails the process of…