简体   繁体   中英

Quickest way to add Carthage in Xcode Project

What is quickest way to add dependencies in Xcode project using Carthage.

How to add or edit dependencies later.

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

Carthage flow

[iOS Dependency manager]

TL;DR:

Download dependency -> build fat binary -> it should  be imported -> slice for release

Long version:

  1. Installing Carthage

    //Homebrew brew install carthage
  2. Create Cartfile file at the project( .xcodeproj )/workspace( .xcworkspace ) directory

  3. Modify Cartfile . Add necessary dependency == repo

     github "<owner>/<repo>" == <version>
  4. Run carthage update under Cartfile location. 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 - (+current consumer)this flag also builds all `shared frameworks schemes` of a consumer workspace/project(in addition to `Carthage/Checkouts` folder) } } }
  5. Drag and drop builded frameworks to General -> Frameworks and Libraries

     //framework location <cartfile_path>/Carthage/Build/
  6. Run the next script. This step is important because carthage build a fat binary(arm64... + x86_64) using lipo [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

*Any carthage command should be called from Cartfile folder

If you run into some error

This usually indicates that project itself failed to compile. Please check the xcodebuild log for more details: /var/folders/2q/jhrcxkmx49g21lydqfrf26ph0000gn/T/carthage-xcodebuild.AySUH3.log

//you can use open command to review the log
open /var/folders/2q/jhrcxkmx49g21lydqfrf26ph0000gn/T/carthage-xcodebuild.AySUH3.log

Crear Carthage cache

rm -rf ~/Library/Caches/org.carthage.CarthageKit

Get stuck on update

carthage update --platform ios --verbose --new-resolver

//e.g.
//The dependency graph contained a cycle

Use XCFramework [About] --use-xcframeworks

//error
Building universal frameworks with common architectures is not possible. The device and simulator slices for "<schema_name>" both build for: arm64
Rebuild with --use-xcframeworks to create an xcframework bundle instead.

//solution
carthage update --platform ios --verbose --new-resolver --use-xcframeworks

[Carthage --no-use-binaries]

[Fast Carthage build]

[Local Carthage]

  • Install carthage on your mac: brew install carthage
  • Create a Cartfile in the project root directory, eg vim Cartfile and paste:
     github "AFNetworking/AFNetworking" ~> 4.0
  • Build the frameworks: Run carthage update --platform iOS
    • Warning: dependending on the library you are using, you'll need to follow their instructions on what carthage update command to run specifically. You may need to run eg carthage update --use-xcframeworks --platform iOS --no-use-binaries instead. Check their readme.
  • Add it to Xcode: In finder, navigate to $project_dir/Carthage/Build . You will find a FrameworkName.framework or FrameworkName.xcframework . Drag the .framework / .xcframework into your Xcode Project (in the project navigator).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM