Lets write our cocoapod and publish it to cocoapods.org
iOS Developer level : Intermediate
Configuration when this article was written
Xcode : Version 11.5
MacOS : 10.15.5 (Catalina)
A working example
Final code can be found in the Resources section below.
What is a cocoapod ?
CocoaPods is an application level dependency manager for the Objective-C, Swift and any other languages that run on the Objective-C runtime, such as RubyMotion, that provides a standard format for managing external libraries.
CocoaPods focuses on source-based distribution of third party code and automatic integration into Xcode projects.
Enough of introduction , let’s be technical…
So to start let us create a framework using xcode.
Xcode -> New -> Project -> Framework
Name It as TTDeviceIdentifier
Inside create a file called DeviceIdentifier
Add following code
import UIKit
public struct DeviceIdentifier {
public static func getDeviceUID() -> String {
if let deviceId = UIDevice.current.identifierForVendor?.uuidString {
return deviceId
}
return ""
}
}
Build once
Next
Create a repo in github TTDeviceIdentifier
Add Readme and Lisence (Select MIT or anything appropriate, I chose MIT)
Now in Terminal cd /Users/………/MY_Frameworks/cocoapods_learning
git clone https://github.com/TeaTalkInternal/TTDeviceIdentifier.git
again cd to TTDeviceIdentifier
ls
you will see Readme and Lisence files
Now Copy the TTDeviceIdentifier framework you created into current folder (/Users/………/MY_Frameworks/cocoapods_learning/TTDeviceIdentifier)
In Terminal now
git add .
git commit -m "Added TTDeviceIdentifier framework"
git push origin master
git tag 1.0.0
git push --tags
Verify in browser if code is available https://github.com/TeaTalkInternal/TTDeviceIdentifier.git
Now Again Cd TTDeviceIdentifier (do ls -al to see TTDeviceIdentifier.xcodeproj there)
pod init
pod spec create TTDeviceIdentifier
ls -al
you see this
-rw-r--r-- 1 kevin.saldanha 1645257511 252 Aug 11 16:30 Podfile
drwxr-xr-x@ 5 kevin.saldanha 1645257511 160 Aug 11 16:23 TTDeviceIdentifier
-rw-r--r-- 1 kevin.saldanha 1645257511 6321 Aug 11 16:49 TTDeviceIdentifier.podspec
drwxr-xr-x@ 5 kevin.saldanha 1645257511 160 Aug 11 16:19 TTDeviceIdentifier.xcodeproj
open -a xcode TTDeviceIdentifier.podspec
Modify to look like below
Pod::Spec.new do |spec|
spec.name = "TTDeviceIdentifier"
spec.version = "1.0.0"
spec.summary = "Cocoapod for managing network calls in swift."
spec.description = "A Cocoapod for managing network calls in swift."
spec.license = "MIT"
spec.homepage = "https://github.com/TeaTalkInternal/TTDeviceIdentifier/"
spec.author = { "Kevin Vishal" => "vishalkevin11@gmail.com" }
spec.social_media_url = "https://twitter.com/vishalkevin11"
spec.swift_version = '5.0'
spec.platform = :ios, "12.0"
spec.source = { :git => "https://github.com/TeaTalkInternal/TTDeviceIdentifier.git", :tag => "1.0.0" }
spec.source_files = "TTDeviceIdentifier/**/*.{h,m,swift}"
end
pod spec lint
You should get something like this
-> TTDeviceIdentifier (1.0.0)
- NOTE | xcodebuild: note: Using new build system
- NOTE | xcodebuild: note: Building targets in parallel
- NOTE | [iOS] xcodebuild: note: Planning build
- NOTE | [iOS] xcodebuild: note: Constructing build description
- NOTE | [iOS] xcodebuild: warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App')
Analyzed 1 podspec.
TTDeviceIdentifier.podspec passed validation.
Now our pod is actually ready . we can Test by adding an example project. This example project can be shipped into repo
So now create a simple viewcontroller base application using xcode and save your project here
Xcode -> New -> Project -> Single View App
Name it Example
And Save here
(/Users/………/MY_Frameworks/cocoapods_learning/TTDeviceIdentifier)
Now cd Example
pod init
open -a xcode Podfile
add below line after # Pods for Example
pod "TTDeviceIdentifier", :path => "../TTDeviceIdentifier"
Now in terminal ‘pod install’
open -a xcode Example.xcworkspace
In ViewController.swift added the following in viewDidLoad()
print(DeviceIdentifier.getDeviceUID())
and add import TTDeviceIdentifier at top.
So your ViewController looks like this
import UIKit
import TTDeviceIdentifier
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
print(DeviceIdentifier.getDeviceUID())
}
}
Run the project you should see UUID in xcode console
cd ..
git add .
git commit -m "Ädded Sample Project"
git push origin master
cd TTDeviceIdentifier
ls
Podfile
TTDeviceIdentifier.podspec
TTDeviceIdentifier
TTDeviceIdentifier.xcodeproj
pod trunk register ke*****in@gmail.com 'Kevin Vishal' --description='macbook pro'
you will be asked to complete your registration
pod trunk push
you should get this
🎉 Congrats
🚀 TTDeviceIdentifier (1.0.0) successfully published
📅 August 11th, 17:48
🌎 https://cocoapods.org/pods/TTDeviceIdentifier
👍 Tell your friends!
Now your pod is up
You can create a xcode project
Xcode -> New -> Project -> Single View App
Name it SampleApp
Save it in Desktop
In terminal
cd to the project folder cd ˜/Desktop/SampleApp
pod init
open -a xcode Podfile
add below line after # Pods for SampleApp
pod 'TTDeviceIdentifier', '~> 1.0'
Now in terminal ‘pod install’
You have the pod installed
open -a xcode SampleApp.xcworkspace
We stop here ...
https://cocoapods.org/pods/TTDeviceIdentifier
https://github.com/TeaTalkInternal/TTDeviceIdentifier/
My Reference :
https://www.youtube.com/watch?v=oZSZ8mievUU
Comments
Post a Comment