iOS總結2-創建遠程私有組件庫及添加資源文件

(一)、自做的遠程索引庫

source 'https://github.com/UserZhangXiaoZhe/ZZMakeSpecs.git'

source 'https://github.com/CocoaPods/Specs.git'

索引庫

https://github.com/UserZhangXiaoZhe/ZZMakeSpecs.git

視頻播放庫 (完成)

版本:1.0.0

pod 'ZZMakeAVPlayer'

https://github.com/UserZhangXiaoZhe/ZZMakeAVPlayer.git

 

FMDB數據庫工具(完成)

版本:0.0.1

pod 'ZZMakeFMDBManager'

https://github.com/UserZhangXiaoZhe/ZZMakeFMDBManager.git

 

iOS WIFI電腦給手機傳文件 (完成)

版本:1.0.0

pod 'ZZMakeWiFiManager'

https://github.com/UserZhangXiaoZhe/ZZMakeWiFiManager.git

 

自定義類別(完成)

版本:1.0.1

pod 'ZZMakeCategory'

https://github.com/UserZhangXiaoZhe/ZZMakeCategory.git

 

(二)、詳細步驟:

1.github上創建ID,添加一個公開的索引庫ZZMakeSpecs.

2.終端上查看本地索引庫:

pod repo, 目前本地僅有github上的那個公有索引庫.

3.終端添加私有索引庫:

// pod repo add 索引庫名稱 索引庫地址

pod repo add ZZMakeSpecs https://github.com/UserZhangXiaoZhe/ZZMakeSpecs.git

現在有2個本地索引庫。

4.github創建組件庫 ZZMakeAVPlayer

5.到合適的位置創建一個與組件名相同的文件夾,

cd進去後,使用如下命令

// pod lib create 組件名

pod lib create ZZMakeAVPlayer

按需回答一些問題

6.設置git憑證:

Name:###

E-mail:###

7.前往你創建的文件夾

把基礎組件相關的東西丟到Classes文件夾中,並且把ReplaceMe.m文件刪除

默認Classes文件夾中存放的文件就是pod install時要下載下來的文件,當然可以通過修改spec文件的配置來更改位置.

8.修改Spec:

s.name             = 'ZZMakeAVPlayer'

  s.version          = ‘1.0.0’                           每次更新更改

  s.summary          = 'ZZMakeAVPlayer.'

  s.description      = <<-DESC

ZZMakeAVPlayer一個播放視頻的工具

                       DESC

  s.homepage         = 'https://github.com/UserZhangXiaoZhe/ZZMakeAVPlayer'

  s.source           = { :git => 'https://github.com/UserZhangXiaoZhe/ZZMakeAVPlayer.git', :tag => s.version.to_s }

  s.source_files = 'ZZMakeAVPlayer/Classes/**/*.{h,m}'

s.ios.deployment_target = '10.0'

s.dependency 'AFNetworking', '~> 2.3'

 

修改完要cd 到Example文件下,pod install

9.cd 到Example文件下,

pod install ,關閉程序重新打開,編譯運行確保組件的可用。

依賴會自動添加進去

10.將代碼提交到組件倉庫.  每次更新從這就行

//cd 到ZZMakeAVPlayer文件下

git init

git add .

git commit -m ‘第一次添加組件’

git remote add origin https://github.com/UserZhangXiaoZhe/ZZMakeAVPlayer.git

git push origin master

 

輸入用戶密碼

(注意:"Please tell me who you are"問題

進入項目根目錄,裏面有一個.git的隱藏文件,使用  ls -a  命令可以看到,

cd .git進文件,有一個config文件,vim config 編輯文件.

編輯config文件,在最後添加如下內容:

[user]

name = ###

email = ###

敲esc鍵-->打出:冒號-->分別敲w  q-->敲回車 ,保存退出

)

 

11.打標籤,標籤0.1.0與spec中的s.version保持一致,提交一次數字增加一次

git tag ‘1.0.0’

git push --tags

12.提交podspec到私有索引庫,先做一個驗證
本地驗證Spec的必填字段

// 本地驗證不會驗證 s.source 中的tag  ,  cd 到ZZMakeAVPlayer.podspec路徑下

pod lib lint ZZMakeAVPlayer.podspec --allow-warnings

遠程驗證

// 遠程驗證會驗證 s.source 中的tag,如果此時沒有打上相應的標籤則會報錯

pod spec lint  --allow-warnings

驗證私有庫提示,如果驗證的是私有庫,則在後面加上--private,否則會有警告,你可以選擇--allow-warnings來忽略該警告

pod lib lint --private

pod spec lint --private

 

13.驗證成功提交podspec文件到索引庫,會有各種報錯

// pod repo push 私有索引庫名稱 spec名稱.podspec

//pod repo push ZZMakeSpecs ZZMakeAVPlayer.podspec

 

//最好加上私有庫地址和官方庫地址

pod repo push --sources=https://github.com/UserZhangXiaoZhe/ZZMakeSpecs.git,https://github.com/CocoaPods/Specs.git ZZMakeSpecs ZZMakeFMDBManager.podspec

--verbose 添加後面可查看詳細

操作過程:先將我們的代碼直接push到本地索引庫ZZMakeSpecs,推送後會自動幫我們同步到遠程索引庫。

 

1.報錯:[!] The repo `MySpecs` at `../../../.cocoapods/repos/ZZPublicSpecs` is not clean

解決方法:

1》移除本地私有索引庫:pod repo remove ZZPublicSpecs

2》添加本地私有索引庫:pod repo add ZZMakeSpecs https://github.com/UserZhangXiaoZhe/ZZMakeSpecs.git

3》添加本地podspec文件到遠程私有索引庫即可:pod repo push ZZMakeSpecs ZZMakeAVPlayer.podspec --allow-warnings

 

2.報錯:Your configuration specifies to merge with the ref 'refs/heads/master' from the remote, but no such ref was fetched.

創建遠端索引庫的時候沒有創建readme.md文件,也就是是個空倉庫。在提交的時候一直報錯卡在更新索引庫這步。網上找了半天也沒找到原因,最後重新生成了一個倉庫,添加了readme.md文件。在去提交索引庫就可以了。先前那個提交一直報錯的那個,我複製了個readme.md文件提交上去,然後再去提交索引庫就可以了。所以創建索引庫的時候一定不能是個空倉庫裏面有啥都行就是不能爲空。

解決辦法:隨便往倉庫放寫東西。

 

3.報錯:Failed to open TCP connection to raw.githubusercontent.com:443

在safari瀏覽器裏訪問以下網址,再執行就沒問題了。

 

4.報錯:failed to push some refs to

通過以下命令進行代碼合併

git pull --rebase origin master

這時候就可以在本地文件內看到README.md文件

之後再運行git push origin master 進行推送

 

5.報錯:[!] The `xxx.podspec` specification does not validate. 但是沒有ERROR

執行:pod repo push --sources=https://github.com/UserZhangXiaoZhe/ZZMakeSpecs.git,https://github.com/CocoaPods/Specs.git ZZMakeSpecs ZZMakeFMDBManager.podspec  --use-libraries --allow-warnings

 

6.報錯[!] Unable to add a source with url `` named `-1`.

解決:給索引庫都添加上路徑,帶上官方的路徑

pod repo push --sources=https://github.com/UserZhangXiaoZhe/ZZMakeSpecs.git,https://github.com/CocoaPods/Specs.git ZZMakeSpecs ZZMakeFMDBManager.podspec

 

14.測試下搜索我們的組件

pod search 'ZZMakeAVPlayer'

如果找不到,請輸入如下命令:

pod setup

命令執行完畢後,如果還是搜索不到,請刪除spec本地索引庫,重試一遍

// 刪除索引文件:

rm ~/Library/Caches/CocoaPods/search_index.json

// 刪除成功後再次搜索:

pod search

 

15。添加Podfile文件

pod init

vim Podfile 進入文件,點擊i開始編輯

 

在Podfile的最頂部添加如下描述

// 第二行是爲了保證公有庫的正常使用

source 'https://github.com/UserZhangXiaoZhe/ZZMakeSpecs.git'

source 'https://github.com/CocoaPods/Specs.git'

添加使用組件ZZMakeAVPlayer

pod 'ZZMakeAVPlayer'

敲esc鍵-->打出:冒號-->分別敲w  q-->敲回車 ,保存退出

安裝

pod install

(三)、其他

刪除索引庫

pod repo remove master

 

問題 1. 維護階段(擴充代碼,版本號升級)

需重新提交代碼 修改spec描述文件,大概步驟如下:

1. cd 到.spec文件下,重新提交代碼

    git add .

    git commit -m ‘更改’

    git push origin master

2. 改spec描述文件,打tag

    git tag 1.0.1

    git push --tags

3. 提交pec文件

4. 更新spec文件到索引庫

    pod repo push ZZMakeSpecs 文件名稱.podspec

 

問題 2. 依賴關係解決(比如製作庫需要依賴AFN)

//直接spec文件描述即可

s.dependency 'AFNetworking', '~> 3.1.0'

問題 3. 依賴關係子庫解決

(如一個庫裏有三個子庫,現在只需用到其中一個,不要全部用到 ,即把一個庫分成幾個小庫)

# 代表分離子庫

#s.source_files = 'xx/Classes/**/*'  這種寫法是匹配所有的

 

s.subspec 'Category' do |c|

    c.source_files = 'xx/Classes/Category/**/*'

end

 

s.subspec 'Network' do |n|

    n.source_files = 'xx/Classes/Network/**/*'

    n.dependency 'AFNetworking', '~> 3.1.0'

end

 

s.subspec 'Tool' do |t|

    t.source_files = 'xx/Classes/Tool/**/*'

end

 

然後使用這個子庫過程podefile 寫法

target 'Example' do

   use_frameworks!

    pod 'xx/Category'

    pod 'xx/Network'

end

 

Search  pod搜索不到

1.先移除掉本地的master,在終端輸入pod repo remove master

2.然後轉到路徑下,在終端輸入cd ~/.cocoapods/repos

3.再把github上的spec下載下來,終端輸入命令:git clone --depth 1 https://github.com/CocoaPods/Specs.git master

4.再執行pod install --no-repo-update就能夠正確更新這些第三方庫了.

 

 

(四)、給自己的組件添加資源文件

1.如何把資源文件打包爲.bundle文件?

通常我們都會用.bundle文件把資源文件打包,這裏也一樣,把你的圖片、音頻、NIB等資源文件先統一放在一個文件夾裏,文件夾名稱最好是你的組件名稱加上bundle,然後修改文件夾後綴爲.bundle,就得到了一個bundle文件;然後把.bundle文件放在與Classes同一級的文件夾下.

Pod會自動添加到組件裏

2.如何在podspec文件裏配置資源路徑?

然後在podspec文件裏通過s.resource指定你的資源文件路徑,如果你的組件名稱叫ZZMakePlayer,那麼你的bundle名稱就可以叫ZZMakePlayerBundle.bundle

在podspec文件裏配置:

  s.resource     = 'ZZMakePlayer/ZZMakePlayerBundle.bundle'

 

3.如何讀取bundle裏面的資源?

首先我們需要獲取bundle,你是不是立即想到了NSBundle mainBundle ? 但是當你的 pod 庫以 framework 形式被使用時,你的資源不是被拷貝到 mainBundle 下,而是被放到 pod 的最終產物—— framework裏。此時,你必須保證自己在訪問這個 framework 的 bundle,而不是主工程的。

創建了一個NSBundle的類別文件

先獲取framework 的 bundle,ZZAVPlayerView.h是組件裏的某個類文件:

@interface NSBundle (ZZAVPlayerBundle)

+ (instancetype)getCurrentBundle;

@end

 

#import "ZZAVPlayerView.h"

@implementation NSBundle (ZZAVPlayerBundle)

+ (NSURL *)zzAVPlayerResourcesBundleURL {

    //先獲取framework 的 bundle

    NSBundle *bundle = [NSBundle bundleForClass:[ZZAVPlayerView class]];

    return [bundle URLForResource:@"ZZMakePlayerResources" withExtension:@"bundle"];

}

+ (instancetype)getCurrentBundle {

    //再獲取我們自己手動創建的bundle

    return [self bundleWithURL:[self zzAVPlayerResourcesBundleURL]];

}

@end

 

獲取到了我們存放資源文件的bundle文件以後,就可以訪問資源文件了,

創建一個類別:

@interface UIImage (ZZAVPlayerImage)

+ (UIImage *)getImageWithNamed:(NSString *)imageName;

@end

 

#import "NSBundle+ZZAVPlayerBundle.h"

@implementation UIImage (ZZAVPlayerImage)

+ (UIImage *)getImageWithNamed:(NSString *)imageName{

    NSString *realName = [NSString stringWithFormat:@"%@@2x",imageName];

    NSString *path = [[NSBundle getCurrentBundle] pathForResource:realName ofType:@"png"];

    UIImage *loadingImage = [[UIImage imageWithContentsOfFile:path] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    return loadingImage;

}

@end

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章