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閱讀 229,517評論 6 539
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,087評論 3 423
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,521評論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,493評論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,207評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,603評論 1 325
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,624評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,813評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,364評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,110評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,305評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,874評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,532評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,953評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,209評論 1 291
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,033評論 3 396
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,268評論 2 375

推薦閱讀更多精彩內容