Swift uses Carthage to install and manage third-party libraries

Source: Internet
Author: User

Previously, I introduced how to use CocoaPods to manage third-party libraries (Swift-CocoaPods installation and usage instructions). This article introduces another third-party library management tool: Carthage.

1. Carthage introduction

Compared with the long history of CocoaPods, Carthage is still relatively young and has not been around for a few years since the Swift language came out ). The goal is to add a third-party framework (frameworks) to our project in the simplest way.
GitHub home: https://github.com/Carthage/Carthage.git

2. Differences between Carthage and CocoaPods

(1) to use CocoaPods, we only need to modify the Podfile file. CocoaPods directly creates and modifies the workspace configuration of the project. That is to say, CocoaPods creates a highly integrated project.
Carthage features flexibility and low coupling. You do not need to integrate the corresponding project or create a new workspace during use. You only need to rely on the packaged framework file.

(2) Although CocoaPods is more powerful, many libraries cannot be updated due to network problems in China (so in the previous article, I switched it to the image source provided by Taobao ), or the update is useless, or there are various problems during packaging.
Carthage only needs to download the project from github. The configuration is simple and the use of third-party libraries is as clean and concise as using Apple's native framework.

3. Install Carthage

(1) run the following command in "terminal" to update homebrew
1
Brew update

(2) install Carthage
1
Brew install carthage

(3) after installation, run the carthage version command to view the version.

4. Use of Carthage
(1) first go to the root directory of the project and create a blank Cartfile file.


Cd/Users/hangge/Documents/Code/hangge_1359
Touch Cartfile

(2) open the Cartfile file and write the following content. (Here we will use the Alamofire and SwiftyJSON libraries as examples)

Github "Alamofire/Alamofire" ~> 3.0
Github "SwiftyJSON/SwiftyJSON"

(3) save and close the Cartfile and execute the following command.

Carthage update -- platform iOS

At this time, Carthage will automatically download and compile the required third-party library for us.

(4) after the command is executed, a folder named Carthage will be created in the project folder.

(4) after the command is executed, a folder named Carthage will be created in the project folder.

 


 

 

 

(5) the newly created framework file is in the Carthage/Build/iOS folder.

 


 

 

 

(6) open our project and drag the two Frameworks above to General> Linked Frameworks and Libraries.

 


 

 

 

(7) click the Build Phases tab on the configuration page, and add a Run Script

 


 

 

 

(8) modify the new Run Script as follows:

 


 

Enter the text area below Shell:/usr/local/bin/carthage copy-frameworks

 

Add the library to be imported to input Files:

 

$ (SRCROOT)/Carthage/Build/iOS/Alamofire. framework

 

$ (SRCROOT)/Carthage/Build/iOS/SwiftyJSON. framework

 

 

 


(9) at the end of the development, we only need to import the required Library when using it.


At the end of the development, we only need to import the required Library when using it.

Import UIKit
Import Alamofire
 
Class ViewController: UIViewController {
 
Override func viewDidLoad (){
Super. viewDidLoad ()
        
Alamofire. request (. GET, "http://www.hangge.com ")
. ResponseString {response in
Print ("Success: \ (response. result. isSuccess )")
Print ("Response String: \ (response. result. value )")
        }
    }
 
Override func didReceiveMemoryWarning (){
Super. didReceiveMemoryWarning ()
    }
}

5. Modify or update third-party component libraries

(1) if the library referenced by our project has not changed, just execute the update command to update these libraries to the latest version.

Carthage update -- platform iOS

(2) If you want to add a third-party library or delete the original library. Edit and modify the Cartfile file first. Then execute the update command.

Carthage update -- platform iOS
Finally, open the project and modify the library reference in General-> Linked Frameworks and Libraries. And the relevant content in the input Files in the Run Script.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.