天天看點

iOS元件化實踐(基于CocoaPods)

iOS元件化實踐(基于CocoaPods)
做iOS開發的同學對這張圖檔再熟悉不過了,在使用第三庫的時候,cocoapods确實給我們帶來了極大的友善。那麼,我們如何制作自己的pod呢?下面是之前的實踐筆記

Demo中的元件式樣:

iOS元件化實踐(基于CocoaPods)

cocoapods文檔提供了兩種方法:

  • 方法1 pod lib create YeshifuShareUI
  • 方法2  pod spec create YeshifuShareUI

    兩種方法之前都嘗試過,方法一會幫助你建立一大堆的檔案,包括示範demo建立;方法二友善你在現有的項目中提取你需要制作pod的代碼。

    這裡使用方法2。

在開始之前,我們先注冊下CoocaPods ,pod trunk register ,之後你會收到一份郵件,需要點下裡面連結驗證。

詳細步驟

1 整理代碼

随便找一個現有的項目,把裡面的一個子產品放在同一個檔案夾下,我這裡放在ShareUI檔案夾下面。

iOS元件化實踐(基于CocoaPods)

圖一 項目目錄結構

2 建立 YeshifuShareUI.podspec檔案

在終端cd 到ShareUIDemo (如圖一所示),執行pod spec create YeshifuShareUI ,得到檔案YeshifuShareUI.podspec

3 編輯 YeshifuShareUI.podspec

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

#

#  Be sure to run `pod spec lint YeshifuShareUI.podspec' to ensure 

this

is

a

#  valid spec and to remove all comments including 

this

before submitting the spec.

#

#  To learn more about Podspec attributes see http:

//docs.cocoapods.org/specification.html

#  To see working Podspecs 

in

the CocoaPods repo see https:

//github.com/CocoaPods/Specs/

#

Pod::Spec.

new

do

|s|

# ―――  Spec Metadata  ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

#

#  These will help people to find your library, and whilst it

#  can feel like a chore to fill 

in

it's definitely to your advantage. The

#  summary should be tweet-length, and the description more 

in

depth.

#

s.name         = 

"YeshifuShareUI"

s.version      = 

"0.0.5"

s.summary      = 

"CocoaPods元件化實踐"

# This description 

is

used to generate tags and improve search results.

#   * Think: What does it 

do

? Why did you write it? What 

is

the focus?

#   * Try to keep it short, snappy and to the point.

#   * Write the description between the DESC delimiters below.

#   * Finally, don't worry about the indent, CocoaPods strips it!

s.description  = < 

"MIT"

, :file => 

"FILE_LICENSE"

}

# ――― Author Metadata  ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

#

#  Specify the authors of the library, 

with

email addresses. Email addresses

#  of the authors are extracted from the SCM log. E.g. $ git log. CocoaPods also

#  accepts just a name 

if

you'd rather not provide an email address.

#

#  Specify a social_media_url where others can refer to, 

for

example a twitter

#  profile URL.

#

s.author             = { 

"葉同學"

=> 

"[email protected]"

}

# Or just: s.author    = 

"葉同學"

# s.authors            = { 

"葉同學"

=> 

"[email protected]"

}

s.social_media_url   = 

"http://yeli.studio"

# ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

#

#  If 

this

Pod runs only on iOS or OS X, then specify the platform and

#  the deployment target. You can optionally 

include

the target after the platform.

#

# s.platform     = :ios

#s.platform     = :ios, 

"8.0"

s.ios.deployment_target = 

'8.0'

#指定平台和最低支援版本

#  When using multiple platforms

# s.ios.deployment_target = 

"5.0"

# s.osx.deployment_target = 

"10.7"

# s.watchos.deployment_target = 

"2.0"

# s.tvos.deployment_target = 

"9.0"

# ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

#

#  Specify the location from where the source should be retrieved.

#  Supports git, hg, bzr, svn and HTTP.

#

s.source       = { :git => 

"https://github.com/shiyeli/ShareUIDemo.git"

, :tag => 

"#{s.version}"

}

# ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

#

#  CocoaPods 

is

smart about how it includes source code. For source files

#  giving a folder will 

include

any swift, h, m, mm, c & cpp files.

#  For header files it will 

include

any header 

in

the folder.

#  Not including the public_header_files will make all headers 

public

.

#

#這裡路徑需要注意下,是以YeshifuShareUI.podspec為基準。

#如果你的YeshifuShareUI.podspec檔案在其他層級處建立的,那麼根據自己的情況寫。

#ShareUI正是放置元件代碼的檔案夾

s.source_files  = 

"ShareUIDemo/ShareUIDemo/ShareUI"

"ShareUI*.{h,m}"

#s.exclude_files = 

"Classes/Exclude"

# s.public_header_files = 

"Classes*.h"

# ――― Resources ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

#

#  A list of resources included 

with

the Pod. These are copied into the

#  target bundle 

with

a build phase script. Anything 

else

will be cleaned.

#  You can preserve files from being cleaned, please don't preserve

#  non-essential files like tests, examples and documentation.

#

# s.resource  = 

"icon.png"

# s.resources = 

"Resources/*.png"

# s.preserve_paths = 

"FilesToSave"

"MoreFilesToSave"

# ――― Project Linking ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

#

#  Link your library 

with

frameworks, or libraries. Libraries 

do

not 

include

#  the lib prefix of their name.

#

s.framework  = 

"UIKit"

# s.frameworks = 

"SomeFramework"

"AnotherFramework"

# s.library   = 

"iconv"

# s.libraries = 

"iconv"

"xml2"

# ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

#

#  If your library depends on compiler flags you can 

set

them 

in

the xcconfig hash

#  where they will only apply to your library. If you depend on other Podspecs

#  you can 

include

multiple dependencies to ensure it works.

s.requires_arc = 

true

# s.xcconfig = { 

"HEADER_SEARCH_PATHS"

=> 

"$(SDKROOT)/usr/include/libxml2"

}

# s.dependency 

"JSONKit"

"~> 1.4"

end

對于其他配置,根據需要,删删改改依葫蘆畫瓢就好。

4 送出項目代碼到github遠端倉庫

依次執行:

1 2

git add .  && git commit -m

'配置podspec'

git tag 

0.0

.

5

&& git push --tags

5 驗證YeshifuShareUI.podspec 是否正确

1

pod lib lint

iOS元件化實踐(基于CocoaPods)

6 送出到CocoaPods

1

pod trunk push YeshifuShareUI.podspec

Success !

iOS元件化實踐(基于CocoaPods)

完畢之後在CocoaPods搜尋試試看