iOS-Carthage installation and use (new version)

Thinking of the dependency management of iOS/OS X projects, the first thing that comes to mind must be the famous CocoaPods. The following is the difference between the two in the introduction of Carthage:

CocoaPods (default) automatically builds and updates an Xcode workspace to manage your projects and all dependencies. Carthage uses xcodebuild to compile binary libraries, and the rest of the integration work is left to the developers.

CocoaPods are easy to use, Carthage is more flexible and not too invasive to existing projects.

CocoaPods hopes to build an ecosystem that makes it easier to discover and integrate third-party codebases. Carthage hopes to become a decentralized dependency management system that does not provide a centralized project list, reducing maintenance costs and the probability of a single point of failure. However, this makes it inconvenient for developers to find projects.

CocoaPods projects need to configure the podspec file, which contains information about the project and third-party libraries. Carthage does not use a similar configuration file, the dependencies of third-party libraries are configured through Xcode projects.

 

Install Carthage

1. Directly download the installation package: address , but the file host is on amazon S3, I have tried several times without success. . .

2. Using Homebrew

brew update
brew install carthage

 

Compile third-party libraries

1. Create a Cartfile to list the third-party library information you want to use, the following is a simple example

Construct two libraries:

 
  
github "AFNetworking/AFNetworking" ~> 3.0
 github "Monsory/Monsory"
//If no version number is specified, the default is the latest version

2. Run carthage update

carthage update

3. At this time, the following file directory will be generated

copy code
 |-Carthage
 |---Build
 |-----Mac
 |-------Mantle.framework
 |---------Versions
 |-----------A
 |-------------Headers
 |-------------Modules
 |-------------Resources
 |-------ReactiveCocoa.framework
 |---------Versions
 |-----------A
 |-------------Headers
 |-------------Modules
 |---------------ReactiveCocoa.swiftmodule
 |-------------Resources
 |-----iOS
 |-------Mantle.framework
 |---------Headers
 |---------Modules
 |---------_CodeSignature
 |-------ReactiveCocoa.framework
 |---------Headers
 |---------Modules
 |-----------ReactiveCocoa.swiftmodule
 |---------_CodeSignature
 |---Checkouts
 |-----libextobjc
 |-------Configuration
 |---------Base
 |-----------Configurations
 |-----------Targets
 |---------Mac OS X
 |---------iOS
 |-------Tests
 |-------extobjc
 |-------extobjc.xcodeproj
 |---------project.xcworkspace
 |---------xcshareddata
 |-----------xcschemes
 |-----xcconfigs
 |-------Base
 |---------Configurations
 |---------Targets
 |-------Mac OS X
 |-------iOS
copy code

Carthage/Checkouts directory: source code obtained from github

Carthage/Build directory: compiled Framework binary code base

 

4. Open the project, and in a Target -> Build Phases -> Link Library with Libraries of the project, drag and drop the Framework library you want to import in the Carthage/Build directory.

 

5. To add additional scripts to compile, click "+" -> New Run Script Phase

 

add script

/usr/local/bin/carthage copy-frameworks

添加"Input Files"

$(SRCROOT)/Carthage/Build/iOS/Mantle.framework
$(SRCROOT)/Carthage/Build/iOS/ReactiveCocoa.framework

If you are using a version before 0.11


If you are using a version after 0.11

 

6. Use third-party libraries in your project

copy code
#import "AppDelegate.h"
#import <AFNetworking/AFNetworking.h>


@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    
    return YES;
}
copy code

 

7. , Compile and run the project

 

You're done!

Carthage uses an extremely streamlined way to manage third-party libraries, compiling source code into Framework binaries, and then letting developers take care of library management, importing, and more. Compared with CocoaPods, it is less intrusive to existing projects, and it is also very convenient to use.

      First of all, CocoaPods will directly create and modify the workspace configuration of the project. Everything is for convenience. We only need to modify the pod file and do not need to care too much about other things. CocoaPods creates a highly integrated project. Carthage is characterized by flexibility and low coupling. It does not need to integrate the corresponding project or create a workspace, but only needs to rely on the packaged framework file.

     Secondly, CocoaPods has a lot more functions than Carthage. In China, due to the wall, we have changed it to the source of Taobao to update CocoaPods. Believe me, if you don’t go over the wall, many things still can’t be used and can’t be updated. , version errors and other reasons will make you have to give up some third-party libraries that seem to be very useful. Carthage seems to only need to download the project from github, the configuration is simpler, the project is clean when used, all the third-party libraries are as good as Apple's native framework, and you no longer need to install new CocoaPods libraries. Don't use it, don't spend a lot of time fixing various problems when packaging with CocoaPods, if you have used CocoaPods, when you start using Carthage, you will fall in love with this tool.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326846598&siteId=291194637