2016年2月18日 星期四

Carthage好用到回不去了

Carthage跟Cocoapods不同,它是單純的幫你把framework準備好,要用的你再自己拉進去你的project之中,再也不用看到額外的workspace檔案了!

跟Cocoapods 一樣,Carthage也需要一個Cartfile來告訴它你想生成那些framework。
不過因為Carthage目前只支援git,所以格式也相對簡單
github "ReactiveCocoa/ReactiveCocoa"
這樣就可以抓到最近版的framework,但你也可以指定你要的版本或是branch:
# Require version 2.3.1 or later
github "ReactiveCocoa/ReactiveCocoa" >= 2.3.1

# Require version 1.x
github "Mantle/Mantle" ~> 1.0    # (1.0 or later, but less than 2.0)

# Require exactly version 0.4.1
github "jspahrsummers/libextobjc" == 0.4.1

# Use the latest version
github "jspahrsummers/xcconfigs"

# Use the branch
github "jspahrsummers/xcconfigs" "branch"

# Use a project from GitHub Enterprise
github "https://enterprise.local/ghe/desktop/git-error-translations"

# Use a project from any arbitrary server, on the "development" branch
git "https://enterprise.local/desktop/git-error-translations2.git" "development"

# Use a local project
git "file:///directory/to/project" "branch"
等你準備好Cartfile之後,用terminal在你的專案資料夾鍵入
carthage update --platform iOS
它就會幫你把framework建好放在./Carthage/Build的資料夾中(framework的source code會在./Carthage/Checkouts中)。
你將Build資料夾中的framework拉進你的Xcode的Linked Frameworks and Libraries之中就可以用了。

最後,雖然這樣就可以使用,但如果你想要Run在Simulator時會發現出現如下的錯誤
Command /bin/sh failed with exit code 1

所以如果你想要用Simulator做測試的話,要多做下列的步驟:
  1. 到你的application的target頁面選Build Phrases頁籤。
  2. 按下"+"選"New Run Script Phase"新增一個Run Script。
  3. 在shell下的黑色區域,輸入下列指令/usr/local/bin/carthage copy-frameworks
  4. 最後在input files把你的frameworks的部份加進去
$(SRCROOT)/Carthage/Build/iOS/YourFramework1.framework
$(SRCROOT)/Carthage/Build/iOS/YourFramework2.framework

大功告成,現在你可以看到Simulator彈出來嘍!

P.S.你也可以在update時使用下列參數
carthage update --platform iOS --use-submodules --no-use-binaries

第一個參數說明你只要建立iOS平台的資料夾。
第二個參數讓這個專案以git submodules的型態加入。
第三個參數確保你的framework是在本機Build出來的。

P.S.你也可以用bootstrap指令來抓frameworks
bootstrap和update的差別在於:
update是從你寫的Cartfile中抓取你想到的framework之後,才建立Cartfile.resolved檔案。
而bootstrap是不管你的Cartfile,直接去讀上次建立的Cartfile.resolved檔案,來抓frameworks。

沒有留言:

張貼留言