天天看點

iOS子產品化:基礎架構的建立

現在大家都在推進子產品化,每次粘貼拷貝煩了,我也搞個子產品化,因為公司要新起項目,我想我不能每一次都來粘貼一次這麼多的基礎庫檔案,我直接子產品化出來好了,每次要用pod下就好了,還可以持續更新。

首先我們來說下這樣做的好處,特别是多人開發的時候,這樣是特别友善的,還有就是代碼幹淨,工程裡隻有業務代碼,然後業務代碼再根據功能進行子產品,我這裡還沒弄,項目不大不太需要。主要是在大工程這樣的好處更大,清晰,問題好定位,代碼幹淨解耦,同時也鍛煉你的抽象能力吧。

這次我抽象出來的項目叫“KZWFoundation”,其中包含了基礎控件的統一風格封裝,基礎網絡架構的封裝(對af 3.0的再次封裝)包含加密驗簽,router,基礎controller,通用控件視圖,基礎宏,和一些基礎category的封裝。

下面說下,怎麼建立自己的cocospod庫:

1、将你需要的檔案和demo項目建立在github:

iOS子產品化:基礎架構的建立

第一步,建立.png

這裡借用了James大叔的圖,人懶就不重複截圖了。

2、說下共享吧:

1

pod spec create KZWFoundation

編輯.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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

#

#  Be sure to run `pod spec lint KZWFoundation.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         = 

"KZWFoundation"

s.version      = 

"1.1.0"

s.summary      = 

"iOS 基本庫"

# 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  = <<-DESC

iOS基本庫

DESC

s.homepage     = 

"https://github.com/ouyrp/KZWFoundation"

# s.screenshots  = 

"www.example.com/screenshots_1.gif"

"www.example.com/screenshots_2.gif"

# ―――  Spec License  ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

#

#  Licensing your code 

is

important. See http:

//choosealicense.com for more info.

#  CocoaPods will detect a license file 

if

there 

is

a named LICENSE*

#  Popular ones are 

'MIT'

'BSD'

and 

'Apache License, Version 2.0'

.

#

s.license      = 

"MIT"

# s.license      = { :type => 

"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             = { 

"ouyang"

=> 

"[email protected]"

}

# Or just: s.author    = 

"ouyang"

# s.authors            = { 

"ouyang"

=> 

"[email protected]"

}

# s.social_media_url   = 

"http://twitter.com/ouyang"

# ――― 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"

#  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/ouyrp/KZWFoundation.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

.

#

s.source_files  = 

"Classes/KZWFoundationHear.h"

s.subspec 

'Content'

do

|ss|

ss.source_files = 

'Classes*.{h,m}'

ss.exclude_files = 

"Classes/KZWFoundationHear.h"

ss.framework = 

"MapKit"

end

# 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  = 

"Classes/Rseouce/KZWFundation.bundle"

# s.resource_bundle = 

"Classes/Rseouce/KZWFundation.bundle"

s.resource_bundle = { 

'KZWFundation'

=> [

'Classes/Rseouce/KZWFundation.bundle'

] }

# 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.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 

"Masonry"

s.dependency 

"MJRefresh"

s.dependency 

"AFNetworking"

s.dependency 

"Mantle"

s.dependency 

"MBProgressHUD"

s.dependency 

"SAMKeychain"

end

這裡重點說下,遇到的坑,s.source_files這個說的是你要共享的檔案的位置,和你在建立之後的檔案沒任何關系, s.subspec這個才是建立檔案夾如果你沒有就是預設沒有檔案夾,檔案都在你的庫下面,這樣會很醜很難看,很不優雅,然後s.subspec這個裡面也是一個cocospod是以你需要把你用到的都加上,然後我們說下資源檔案,我們都是優雅的人是以我們肯定不能丢各種png檔案對不對,是以我們需要建立bundle,然後配置是s.resource_bundle = { 'KZWFundation' => ['Classes/Rseouce/KZWFundation.bundle'] }這個浪費我一個下午,是以大家需要看下其他的比較大的庫是怎麼配的不用走彎路。

3、然後是驗證

1

pod lib lint KZWFoundation.podspec --verbose --

use

-libraries --allow-warnings

這個是驗證本地,最好是先跑下這個,沒問題在送出然後驗證遠端,開始我沒這麼做一個天送出了10個版本,被自己蠢死了。

4、送出

1 2 3 4 5 6 7

git add -A && git commit -m 

"version 1.0.0"

git tag 

'1.0.0'

//和上面.podspec一緻

git push --tags

git push origin master

5、驗證遠端的

1

pod spec lint KZWFoundation.podspec --verbose --

use

-libraries --allow-warnings

6、沒問題了之後送出

1 2

pod trunk register *****@xx.com 

"ouyang"

pod trunk push KZWFoundation.podspec  --

use

-libraries --allow-warnings

如果trunk注冊過了可以跳過注冊直接推就好了,成功後就會讓你告訴你的朋友了,這樣就完成了,然後你就可以pod下來了,完美。然後你在改的話,就要升版本,改一次升一次版本。