问题
What is quickest way to add dependencies in Xcode project using Carthage.
How to add or edit dependencies later.
回答1:
Install Carthage
Download Carthage
Open terminal
Terminal: cd ~/Path/To/Folder Containing Project
Create Carthage file as:
Terminal: touch Cartfile
Open Cartfile file from project folder and add required dependency
Example Cartfile file
github "Alamofire/Alamofire" == 4.5
github "Alamofire/AlamofireImage"
After Editing Cartfile file save it.
Run following command from terminal
Terminal: carthage update --platform iOS
xCode > Build phases
- Plus button on top left > New Run Script Phases
- Run Script > Shell script window > add following:
/usr/local/bin/carthage copy-frameworks
- Run Script > Input file window > add following:
$(SRCROOT)/Carthage/Build/iOS/DependencyName.framework
- Link Binary With Libraries > Plus button > Add Other > Navigate to Project Folder > Carthage > Build > iOS > Framework to add
Done
回答2:
Installing Carthage
//Homebrew brew install carthage
Create
Cartfile
file at the project(.xcodeproj
)/workspace(.xcworkspace
) directoryModify
Cartfile
. Add necessary dependency == repogithub "<owner>/<repo>" == <version>
[Note] Any
carthage
command should be called fromCartfile
folderRun
carthage update
. High level logic:`carthage update [dependency]` { - reads `Cartfile`, resolves dependency graph and generates `Cartfile.resolved` to store a list of versions that will be actually built //--no-use-binaries - this flag force a Carthage to not use a `Prebuilt framework` //--no-build - this flag skip a building step - `carthage build` `carthage bootstrap [dependency]` { - reads `Cartfile.resolved` `carthage checkout [dependency]` { `carthage fetch <path>` { - fetch dependency from `Cartfile.resolved` into `~/Library/Caches/org.carthage.CarthageKit/` folder } - checkout/move a dependency from `~/Library/Caches/org.carthage.CarthageKit/` to generated `Carthage/Checkouts` folder } `carthage build [dependency]` { - builds all `shared frameworks schemes` from `Carthage/Checkouts` into generated `Carthage/Build` folder //--no-skip-current - this flag also builds all `shared frameworks schemes` of a consumer workspace/project(in addition to `Carthage/Checkouts` folder) } } }
Drag and drop builded frameworks to
General -> Frameworks and Libraries
Run the next script. This step is important because carthage build process a
fat
binary(arm64... + x86_64) usinglipo
[About]. Apple reject application which uses it. That is why you should add this extra step to cut architecture for simulator(x86_64)Build Phases -> + -> New Run Script phase -> // /usr/local/bin/carthage - path to Carthage, copy-frameworks - command which sets a necessary architecture and copies framework to an application bundle Shell -> /usr/local/bin/carthage copy-frameworks //path to a generated Carthage/Build Input files -> + -> $(SRCROOT)/Carthage/Build/<platform>/<name>.framework
[Fast Carthage build]
[Vocabulary]
[CocoaPods vs Carthage]
来源:https://stackoverflow.com/questions/52714746/quickest-way-to-add-carthage-in-xcode-project