Skip to content

Create private pods framework with Cocoapods #iOS #cocoapods

shaokanp edited this page Dec 28, 2016 · 5 revisions

The general steps of creating a customized pod framework:

  1. Create an iOS framework for the Modularize
  2. Create a .podspec file contains the pod information
  3. Upload the pod framework to a remote repository
  4. Create a private repo to host all of your private pod specs (first time only)
  5. Add Podfile dependency in the project you like to use your framework

Steps

1. Modularize your code into a stand-alone framework

  1. Create a new Xcode project and select Cocoa Touch Framework in the Framework & Library section.
  2. Drag the codes and resources into the newly created framework project. Double check if all files have their target membership set to the current framework.
  3. If Cocoapods is used in the framework, use it as usual.
  4. Create a new remote repository on Github or Bitbucket and name the repo as the pod name you'd like to use for this framework. In this example, it's helpdesk_ios.

2. Create .podspec file

  1. In the framework project folder, enter the following command in Terminal:
pod spec create helpdesk_ios

This creates helpdesk_ios.podspec in the framework folder. 2. Open the .podspec file in editor and fill in the required information:

Pod::Spec.new do |s|

  # basic information
  s.platform = :ios
  s.ios.deployment_target = '9.0'
  s.name = "helpdesk_ios"
  s.summary = "Feedback module for iOS"
  s.requires_arc = true

  # the version needs to be update whenever a new version is generated
  s.version = "0.1.0"

  # you should choose the license you'd like to use. And do not forget to create the LICENSE file in the same folder, containing the license information
  s.license = { :type => "Apache License 2.0", :file => "LICENSE" }

  # Replace with your name and e-mail address
  s.author = { "Shaokan Pi" => "shaokanp@gmail.com" }

  # Replace this URL with your own Github page's URL (from the address bar)
  s.homepage = "https://github.com/shaokanp"

  # Replace this URL with the url of the repository you generated in the previous step
  s.source = { :git => "https://shaokanp@bitbucket.org/seekrtech/helpdesk_ios.git", :tag => "#{s.version}"}

  # You have to specify the pods dependencies required in this framework
  s.dependency 'Moya/RxSwift'
  s.dependency 'Moya-ModelMapper/RxSwift'
  s.dependency 'RxCocoa'
  s.dependency 'RxOptional'
  s.dependency 'RxGesture'
  s.dependency 'Kingfisher', '~> 2.4'
  s.dependency 'NVActivityIndicatorView'

  # In this case, .swift files
  s.source_files = "helpdesk_ios/**/*.{swift}"

  # the resources file types
  s.resources = "helpdesk_ios/**/*.{png,jpeg,jpg,storyboard,xib}"
end

3. Upload the pod framework to a remote repository

To this step, you should already have a framework project, a remote git repository, and a valid .podspec file. Next, we should push the framework to the remote repository. And do not forget to create a tag with the same version number that you specified in the .podspec file.

git add .
git commit -m "version 0.1.0"
git tag 0.1.0
git remote add origin <the repo url> // in case you haven't done this in the previous steps
git push origin master --tags

4. Create a private specs repo (first time only)

It's time to add your Podspec to a private specs repo. If you don't have one, simply create a new repository on Github or Bitbucket. In this example, we named it SeekrPodSpecs.

After creating the specs host repo, we can upload the newly created pod to our private specs repo by:

pod repo add SeekrPodSpecs [Your Specs repo URL]
pod repo push SeekrPodSpecs helpdesk_ios.podspec

5. Update a new pod version

Whenever there is a new version of helpdesk_ios specs, we can simply enter the following command to validate the new version of the spec and push to the specs repo:

pod repo lint # use --allow-warnings to skip warnings
pod repo push SeekrPodSpecs helpdesk_ios.podspec

6. Use the new spec in another project

Finally, we can use our own private spec in our new project. Edit the Podfile of the project as following:

platform :ios, '9.0'

source 'https://github.com/CocoaPods/Specs.git'
source '[SeekrPodSpecs Git URL]'

use_frameworks!

target 'IceCreamShop' do
  pod 'helpdesk_ios', :path => '<path to helpdesk_ios folder>'
end

Adding :path here will make this Pod as an Development Pods which means you can edit the source code directly in the projects which depend on it.

Notes

Creating UIImage

If you'd like to use a image file from a framework, you cannot simply create an UIImage by UIImage(named: "image_name") directly even if it is called in the same same framework project. Instead, you should create an UIImage by:

let image = UIImage(named: imageName, inBundle: NSBundle(forClass: self.dynamicType), compatibleWithTraitCollection: nil)

Note that, in Swift 3, dynamicType is deprecated and should be changed to type(of:). Also, the NSBundle has been renamed to Bundle. To retrieve the current bundle, use the following code instead:

Bundle(for: type(of: self))

References

How to Create a CocoaPod in Swift - by Raywenderlich