iOS模塊化:基礎框架的創(chuàng)建

現(xiàn)在大家都在推進模塊化,每次粘貼拷貝煩了,我也搞個模塊化,因為公司要新起項目,我想我不能每一次都來粘貼一次這么多的基礎庫文件,我直接模塊化出來好了,每次要用pod下就好了,還可以持續(xù)更新。

首先我們來說下這樣做的好處,特別是多人開發(fā)的時候,這樣是特別方便的,還有就是代碼干凈,工程里只有業(yè)務代碼,然后業(yè)務代碼再根據功能進行模塊,我這里還沒弄,項目不大不太需要。主要是在大工程這樣的好處更大,清晰,問題好定位,代碼干凈解耦,同時也鍛煉你的抽象能力吧。

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

下面說下,怎么創(chuàng)建自己的cocospod庫:

1、將你需要的文件和demo項目創(chuàng)建在github:

第一步,創(chuàng)建.png

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

2、說下共享吧:

pod spec create KZWFoundation

編輯.podspec:

#

#? 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" => "1697006288@qq.com" }

# Or just: s.author? ? = "ouyang"

# s.authors? ? ? ? ? ? = { "ouyang" => "1697006288@qq.com" }

# 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這個說的是你要共享的文件的位置,和你在創(chuàng)建之后的文件沒任何關系, s.subspec這個才是創(chuàng)建文件夾如果你沒有就是默認沒有文件夾,文件都在你的庫下面,這樣會很丑很難看,很不優(yōu)雅,然后s.subspec這個里面也是一個cocospod所以你需要把你用到的都加上,然后我們說下資源文件,我們都是優(yōu)雅的人所以我們肯定不能丟各種png文件對不對,所以我們需要創(chuàng)建bundle,然后配置是s.resource_bundle = { 'KZWFundation' => ['Classes/Rseouce/KZWFundation.bundle'] }這個浪費我一個下午,所以大家需要看下其他的比較大的庫是怎么配的不用走彎路。

3、然后是驗證

1

pod lib lint KZWFoundation.podspec --verbose --use-libraries --allow-warnings

這個是驗證本地,最好是先跑下這個,沒問題在提交然后驗證遠程,開始我沒這么做一個天提交了10個版本,被自己蠢死了。

4、提交

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、沒問題了之后提交

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

pod trunk push KZWFoundation.podspec? --use-libraries --allow-warnings

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

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容