Introduce
- In iOs application development, the use of external dependency is essential. This saves us time by taking advantage of components that others have built before. There are many ways to integrate dependency into your project.
- The easiest way is to be able to directly download lib from github and drag the corresponding components into your project. However, such manual handling will take time, not professional. Moreover, these components may be related to other dependency components, making manual handling more difficult
- The second way, we use the dependency management tool Coapods, Carthage .. to build dependency automatically. Thus, the management will be more effective and professional. This article will guide you step by step to build applications with Carthage.
Setting
There are many ways to install Carthage, Option 1: Download the .pkg file at https://github.com/Carthage/Carthage/releases and run the file to install.
Method 2: Install on the terminal using Homebrew.
Install Home brew:
1 2 | ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" |
Update to the latest version
1 2 | brew update |
After installing Homebrew we will install Carthage with the command
1 2 | brew install Carthage |
Build Project
Carthage uses the Cartfile file to list the libraries and versions for integration into the project.
Create Cart file
B1: cd to the project directory
B2: Create Cart file with command
1 2 | nano Cartfile |
B3: list the corresponding libraries into the cart file. For example
B4: Crtl + C to exit and save the file.
File Cart is defined according to OGDL: Ordered Graph Data Language. There are 3 components in declaring a Cart file.
– Dependency origin: To tell the source Carthage to fetch dependency.
- For example: git, github ….
– Name dependency: Name of the denpendency to add to the project.
– Dependency Version: Version you want to add. Syntax
- == 1.0: Use version 1.0
= 1.0: Versions greater than or equal to 1.0
- ~> 1.5: Versions corresponding to 1.5.
Build Dependency
1 2 | carthage update --platform iOS |
keyword –platform iOS to limit build for ios platform. Without this keyword, it will build for all MacOS and Watch
After the build is completed in the project, the Resolve file and the Carthage folder will be created as follows:
Open the Build / iOS folder. Drag the files Alamofire.framework and AlamofireImage.framework in BuildPharse / Link Binary with Libraries
- Add Run Script
Add the following command to the link
1 2 | /usr/local/bin/carthage copy-frameworks |
and input file
1 2 3 4 | $(SRCROOT)/Carthage/Build/iOS/Alamofire.framework $(SRCROOT)/Carthage/Build/iOS/AlamofireImage.framework |
Now the BuildPharse tab will look like this
- Thus, we have completed the configuration and integrated the necessary dependencies into the project. Now we can import and use.