Create and Distribute Private Libraries with Cocoapods

原文链接

In this post I’m going to show you how to develop and distribute in-house private code libraries using Cocoapods.Cocoapodsby the way is an excellent tool for managing third party dependencies in a project. It not only provides a way for easily integrating those dependencies but also allows you create your own dependencies and manage them as well. And best of all, you can it to simplify code sharing inside your organization. This is known as a private pod and is described in great detail on thecocoapods site.

In this blog I’m going to show you a simpler way to create a private pod, which will be easier to understand for beginners as well.

The most important thing you should keep in mind while creating a private pod is that it requires the creation of two repositories. One repository is for the code or classes you want to share (which we call the ‘Pod’) and the other is the ‘Podspec’ repository which includes all the information about that Pod. The Podspec repository needs to be created once for all the pods you create, but the pod repository is a separate repository for each branch of code you want to reuse and share with your team.

Having said that, the first step we need to perform is to create these repositories. Let’s jump right to it.

Step 1: Create your Podspec Repository on Github

First of all, you need to create the private ‘Podspec’ repository. For this you need to create a repository in Github first. To do so:

Go to Github

Create new repo

Select the private option and name your spec repository. In this case we have created a repository named folio3-specs in Github.)

Now run the following commands

echo “# folio3-specs” >> README.md

git init

git add README.md

git commit -m “first commit”

git remote add originhttps://github.com/shahabejaz/folio3-specs.git

git push -u origin master

The above created repository does not hold anything yet other than aReadme.mdfile. We will address this later when we create our first pod.

Step 2: Add your Private Repository to your CocoaPods Installation

Once you’re done with creating a spec repository in Github, you only need to run the following command in the terminal to add your private repository to the cocoapods installation.

pod repo add [REPO_NAME] [SOURCE_URL]

In the above code snippetREPO_NAMEis the name you will use to reference the ‘PodSpec’ repo and SOURCE_URL is the Github url of the repository you just have created. In this case our repository name is folio3-specs and the source URL is the Github URL of that repository.

If everything worked correctly then you should be able to link your spec repository by running the following commands.

cd ~/.cocoapods/repos/REPO_NAME

pod repo lint.

Now that ourpodspecrepository is ready, we can move forward and create our second private repository which is our Pod repo. This repository will hold the code you want to share within the organization.

Step 3: Create your Pod Repository on Github

First we’ll need to create an empty Github repository as specified in Step 1 above, only now, we’ll give it the name of the pod we want to share. So we’ll name the repositoryFLCommonLibrary. This is basically a repository that holds a bunch of utilities and categories that are used in different projects across the organization.


Step 4: Generate the Pod Project

CocoaPods provides a nice utility to help you setup your Pod project along with a test app and testing framework. So to generate your Pod project, just run the following command while standing at your empty github repo directory.

pod lib create [POD_NAME]

where[POD_NAME]is the name of the pod you’re creating and want to share with fellow developers in your organization.

After running the above specified command you will be prompted by an interactive script to select various options for your new Pod project. After selecting those options (as shown below) cocoapod will run ‘pod install’ on the sample project we’ve just created.

The end result will be an XCode workspace that is setup for you to commence Pod development. If you already have some source files to add to the project, you can copy them into the Pod/Classes folder that has been created for you. You’ll also find that a default test app has been created for you where you can write unit tests and view tests for your Pod.

After the completion of this command the.workspaceproject will open up automatically. If it does not, open the .workspace file in the sample project. You will see aReplaceMe.mfile in the pod target as shown below.

This is the location where you will put the files [.h,.m] that you want to share with your pod. You will also see the Podspec Metadata folder as well. Next, we need to edit the podspec file.

Step 5: Edit the Podspec File

Open the newly generatedpodspecfile. Thankfully Cocoapod has generated a well completed pod spec template for us.

A Podspec file, or Spec, describes a version of a Pod library. It includes details about where the source files are located, which files to use, the build settings to apply, dependencies, frameworks used and other general metadata such as the name, version and description for the Pod.

Below is an example of a Podspec generated by cocoapod for our private pod:

Now, open up the terminal again and go to the folder where the .podspec file is located and run the following command without changing anything in the .podspec file.

pod lib lint FLCommonLibrary.podspec

When you do that, you might see the following output.

If you do, it means that something’s wrong with our podspec file. So we’ll need to resolve those issues. To do so, we need to do the following.

Specify the proper summary of our pod

Add some description

Replace the with our Github’s username

After making these changes, run the pod lib lint FLCommonLibrary.podspec again.

Tip:You should also specify the Github username for the s.source field otherwise it will generate errors later on when you push this podspec to your spec repo.

This time you’ll see that the command is successful, as you can see in the screenshot below.

Now, we’re good to go with the podspec file and our pod is ready to have some shareable files added to it. So we’ll do exactly that.

Step 6: Add Code in your Pod

Since we have created some reusable utility classes and extensions which we want to share with our team, we will drag and drop these files in the folder (as depicted below).

Now we want to test these files in the sample project we created. For this we will run the pod install command on our sample project. This will install these files in the sample project’s Pod file. You can include these files in the sample project by using the following command.

.

After testing in the sample project, you are ready to push the pod project and share it with fellow developers in your organization.

Note:Remove the ReplaceMe.m file, it’s of no use any more.

Step 7: Push your Pod in the Specs Repo

Now that you’ve built and tested your Pod, it’s time to deploy it to your private Podspec repository. The first thing you need to do, in order to do this, is tag your Pod’s Git repo.

Step 7a: Tagging

First we will update the pod version in the .podspec file to 1.0.1 and will commit the changes on Github. You can change it to any version that is suitable to you but make sure that it is the same version as your Git tag version.

Now we will tag the code on Github by running following command on our repo from the terminal.

git tag ‘1.0.1’

git push –tags

Step 7b: Push to Spec Repo

After pushing the tag to the pod repo, you need to push this pod to your private spec repo which you created in the first step. To do that, run the following command to send the library your private Podspec repo.

pod repo push [REPO_NAME] [POD_NAME].podspec

In this case, folio3-specs is the repo_name and FLCommonLibrary is the pod_name of our pod. So you should specify (substitute) these names in above code snippet.

Tip:Before running this command you should run the pod spec lint FLCommonLibrary.podspec to validate that your spec is correct.

Now you can see that this pod repo is referenced in your spec repo (as shown below).

Step 8: Share It with Your Team

If you want other members of your development team to be able to use this pod then they should have access to the spec repo, and they must add the private repo to their local Cocoapods installation with the command:

pod repo add [REPO_NAME] [SOURCE_URL]

They also need to specify the source URL of the spec repo at the top of the pod file, so that cocoapods knows where to find the installation of your pod (as shown below).

Congratulations! You have just published your first private Pod with your team. You can now leverage the benefits of reusable code in your internal iOS projects using the power of cocoapods.

Tip: You can get the latest updates in cocoapods by following them on twitter:https://twitter.com/cocoapods

Happy coding and keep sharing :)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,233评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,013评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,030评论 0 241
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,827评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,221评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,542评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,814评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,513评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,225评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,497评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,998评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,342评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,986评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,812评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,560评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,461评论 2 266

推荐阅读更多精彩内容