Note: This tutorial has been writen with Xcode 9, Swift 3.2, MacOS 10.12

Cocoapod Installation

“What exactly is CocoaPods?”
It’s probably best to let the CocoaPods website provide the answer:

CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. It has over 30 thousand libraries and is used in over 1.9 million apps. CocoaPods can help you scale your projects elegantly.

So cocoapods is based on dependency pattern that make adding, removing, updating a lib easy.
CocoaPods is built on Ruby so you just simply install it by gem

sudo gem install cocoapods
//Cloning spec repo master from https://github.com/CocoaPods/Specs.git into ~/.cocoapods/ on your computer
pod setup --verbose

Adding third-party library

Create your new project with xcode then

cd path-to-your-xcode-project
//create pod file
pod init 
//create .xcworkspace file
pod install

From now you have to open projet by .xcworkspace file instead of .xcodeproj file

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
//use_frameworks tells CocoaPods that you want to use Frameworks instead of Static Libraries. Since Swift does not support Static Libraries you have to use frameworks.
//update: from Xcode 9 beta 4 swift static libraries are now supported.You don't have to "use_frameworks!" anymore. The main advantage is faster app startup time
use_frameworks!
target 'MyApp' do
// add new lib with specific branch
    pod 'SwiftyJSON', :git ='https://github.com/acegreen/SwiftyJSON.git', :branch = 'swift3'
//install the pod from git repo sitting on your disk 
//instead of being hosted on the cloud
pod 'SwiftyDropbox', :git = '/Users/yourusername/path/to/repo/SwiftyDropbox-yourusername', :branch = 'mybranch'
// add latest version of lib
    pod 'Alamofire'
//add lib with specific version
    pod 'SwiftSVG', '~ 2.0'
// add lib from your local
pod 'MyLib', :path = 'path-to-mylib'
//add lib to Development Pods then you can modify the lib 
pod 'Google-Maps-iOS-Utils', :path => '../..'
end
//setting for each pod
//in case swift version of added library(swift 4.0) 
//is different from your whole project's (swift 3.2)
//you can set up all in xcode but this snipet will prevent issues from comming
// back when you call pod install or pod update
post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            if ['Alamofire','MyLib'].include? target.name
             config.build_settings['SWIFT_VERSION'] = '4.0'
            end
        end
    end
end

Create your own cocoapod library

It’s probably better to find some well-known third-party libraries to take care all of boring stuff for you.

But sometimes you can’t find a pod with the exact functionality you need, or you may want to optimize your project by separating a large pod into smaller, reusable components.

Fortunately, You can create your own pod, put it in local then you can modify it keep changes even when you call pod install or pod update.

So basically there to 2 steps to do this:

Create a Cocoa Touch framework for your component

Creating a Podspec

A podspec defines the specifications of the Pod! Now let’s make one:

  1. To create a Podspec, navigate to your project file in the Terminal.
  2. Run the following command to create the file: touch project-name.podspec
  3. Now open the file using an editor.
  4. Paste the following code inside the Podspec:
Pod::Spec.new do |s|
  s.name             = 'LoginHelper'
  s.version          = '0.1.0'
  s.summary          = ''
 
  s.description      = Something about this pod
                      
 
  s.homepage         = 'https://github.com/YOUR_GITHUB_USERNAME/LoginHelper'
  s.license          = { :type = 'MIT', :file = 'LICENSE' }
  s.author           = { 'YOUR NAME HERE' = 'YOUR EMAIL HERE' }
  s.source           = { :git ='https://github.com/YOUR GITHUB USERNAME/LoginHelper.git', :tag = s.version.to_s }
 
  s.ios.deployment_target = '10.0'
// enables you to specify building a pod as a //static_framework
  s.static_framework = true

// tell CocoaPods which files you need to clone.
  s.source_files  = "Classes/**/*"
  s.exclude_files = "Assets/*{xib,xcassets,png,jpg,otf,ttf}"
 
end

Put it in your local and add it like I mentioned above

Publish and share it to the world

Pushing to Github

CocoaPods needs a source for the Pod. In most cases, developers use GitHub for this
Then,

  1. Click the releases button.
  2. Create a new release
  3. Set tag
  4. Other optional information
  5. Push release

Linting The Project

CocoaPods requires you to lint your project to verify that nothing is wrong with a project.
To lint your project run the following in your project directory:

pod lib lint

A nature, universe, science, music, love lover

Leave a Reply

Your email address will not be published. Required fields are marked *

Bitnami