iOS relies on the Carthage of Library management tools

Source: Internet
Author: User

In iOS development, we often use cocoapods to manage dependent libraries. Coaoapods is a holistic solution where we specify the Podfile third-party libraries we need, and then CocoaPods download, integrate, and then modify or create the files for our project workspace .

In contrast, Carthage it takes a lot of light, it also needs a Cartfile description file called, but Carthage does not make any modifications to our project structure , not much more workspace . It simply downloads them locally based on the third-party libraries configured in our profile and uses them to xcodebuild build into a framework file. These libraries are then integrated into the project by ourselves. Carthageuse a non-invasive philosophy. As a developer, you must Carthage manually associate the third-party library with the project after it has been built.

In addition to Carthage non-invasive , it is to be centralized, its package management is not like that CocoaPods , there is a central server (cocoapods.org), to manage the meta-information of each package, but rely on each third-party library's own source address, For example Github . This is also the pros and cons, the advantage is that we no longer rely on the management of the central server, not subject to the central server information and stability restrictions (especially in our network access problems), the disadvantage is that we want to find third-party libraries, there is no central server to help us index, Instead, you must look it up on the network itself.

1. Installing Carthage
    • Method One: Open the terminal, enter the following instructions.
$ Brew UpdateBrew install Carthage
    • Method Two: On the Github home page, find: https://github.com/Carthage/Carthage/releases, find carthage.pkg, download and install.

After the installation is complete, you can view the current Carthage version by executing the following command at the terminal:

$ Carthage Version
2. Using Carthage
    • The first step: Enter your project folder via the terminal, as shown in the following example:
CD /users/goflee/desktop/source/swiftdemo/swiftdemo/thirdlibrary
    • Step two: Create an empty Carthage file with the following instructions:
$ Touch Cartfile

A file named Cartfile is created in the project folder.

    • Step three: Use Xcode to open the Cartfile file:
$ open-a Xcode Cartfile

Enter the third-party library you want to import in the open file, as shown in the example:

GitHub "Alamofire/alamofire" "Swiftyjson/swiftyjson"

You can also specify a version number, and the latest version is automatically used when no version number is specified. Specify the version number example:

GitHub "Alamofire/alamofire" ~> 3.4.0
    • Fourth step: Save and close the Cartfile file, execute the following command at the terminal:
$ Carthage Update--platform IOS

Carthage will download and compile the required third-party libraries for you, and when the command is completed, a folder named Carthage will be created in your project folder. The framework and dSYM files appear in the ~/carthage/build/ios, as follows:

"description":

    1. Since it is only necessary to import the libraries under the IOS platform, the platform is specified, and if you need all the libraries, then remove the "--platform iOS" later.
    2. build is stored in a built framework package;
    3. A third-party library project source file is stored in the checkouts .
    • Fifth step: Go back to the project settings, go to the General tab, in the bottom Linked frameworks and Libraries , will Carthage/build/ios in the The framework file is added to the project as shown in:

    • Sixth step: in Build phrases , click the + sign in the upper left corner, add a New Run script Phrase, enter in the script area:
Copy-frameworks

Finally, add the paths of Alamofire and Swiftyjson to Input Files as shown in:

The purpose of adding this run Script is to allow the runtime to find the dynamic library. This is not explicitly stated in the Carthage official documentation. I have actually experimented with this, and if I don't add this copy-frameworks script, the project will crash at startup because it can't find the dynamic library when it's running.

3. Using the Import library

After building and configuring a third-party library, we can simply use it and import it directly into the file to be used, as shown in the following example:

Import Alamofireimport Swiftyjson

iOS relies on the Carthage of Library management tools

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.