{"id":1230,"date":"2015-02-28T11:55:42","date_gmt":"2015-02-28T17:55:42","guid":{"rendered":"http:\/\/dev.iachieved.it\/iachievedit\/?p=1230"},"modified":"2015-02-28T20:57:13","modified_gmt":"2015-03-01T02:57:13","slug":"using-carthage-with-alamofire","status":"publish","type":"post","link":"https:\/\/dev.iachieved.it\/iachievedit\/using-carthage-with-alamofire\/","title":{"rendered":"Using Carthage with Alamofire"},"content":{"rendered":"<p>I have never been a fan of <a href=\"http:\/\/cocoapods.org\/\">CocoaPods<\/a> due to its intrusiveness in the build process and insistence on creating Xcode project workspaces.  Unfortunately managing dependencies through the use of <a href=\"http:\/\/git-scm.com\/book\/en\/v2\/Git-Tools-Submodules\">Git submodules<\/a> is no better.  How many times have you told yourself, &#8220;Okay, I understand submodules now&#8221; only to be met with <code>fatal: reference is not a tree:<\/code> when trying to do a <code>git submodule update<\/code>?  Perhaps I&#8217;m dense, but I shouldn&#8217;t have to turn to Google every time I check out a project which uses submodules.<\/p>\n<p>In the tutorial <a href=\"http:\/\/dev.iachieved.it\/iachievedit\/http-json-requests-with-swift-and-alamofire\">HTTP JSON Request with Swift and Alamofire<\/a> we used the git submodule approach to add the Alamofire framework as a dependency to our Xcode project.  Let&#8217;s now look at a new approach to managing framework dependencies with Carthage.<\/p>\n<p><a href=\"https:\/\/github.com\/Carthage\/Carthage\">Carthage<\/a> was developed to make framework dependency management in your Xcode projects just a bit easier.  Luckily for us, Alamofire now supports using Carthage, and we can leverage that support in our project to get rid of the git submodule approach.<\/p>\n<p>To get started with Carthage, you&#8217;ll first need to install <a href=\"http:\/\/brew.sh\">Homebrew<\/a>.  If you have never used <a href=\"http:\/\/brew.sh\">Homebrew<\/a> before, you&#8217;re in for a real treat.  To install Homebrew open a terminal and paste:<\/p>\n<pre>\r\nruby -e \"$(curl -fsSL https:\/\/raw.githubusercontent.com\/Homebrew\/install\/master\/install)\"\r\n<\/pre>\n<p>Follow the prompts and once Homebrew is properly installed then you can install carthage with <code>brew install carthage<\/code>.<br \/>\n<code><br \/>\n==> Downloading https:\/\/downloads.sf.net\/project\/machomebrew\/Bottles\/carthage-0.5.2.yosemite.bottle.tar.gz<br \/>\nAlready downloaded: \/Library\/Caches\/Homebrew\/carthage-0.5.2.yosemite.bottle.tar.gz<br \/>\n==> Pouring carthage-0.5.2.yosemite.bottle.tar.gz<br \/>\n[emoji beer mug] \/usr\/local\/Cellar\/carthage\/0.5.2: 157 files, 34M<br \/>\n<\/code><\/p>\n<p>Now, let&#8217;s look at how to add Alamofire to our project using Carthage. The first step is to create a <code>Cartfile<\/code> in our project folder. The <code>Cartfile<\/code> is what you might expect: a list of dependencies (and where to fetch them from) for our project. Our <code>Cartfile<\/code> will contain a single dependency on Alamofire.<\/p>\n<pre>\r\ngithub \"Alamofire\/Alamofire\" >= 1.1\r\n<\/pre>\n<p>Run <code>carthage update<\/code>. Carthage will now check out and <i>build<\/i> Alamofire.<br \/>\n<code><br \/>\n% carthage update<br \/>\n*** Fetching Alamofire<br \/>\n*** Checking out Alamofire at \"2f39b8634ab85cd6b3a55bbcfdec8cbb6463a6ee\"<br \/>\n*** xcodebuild output can be found in \/var\/folders\/yn\/ljzrn5hd4g3fxsj4klwkhbkm0000gp\/T\/carthage-xcodebuild.wAPiQR.log<br \/>\n*** Building scheme \"Alamofire iOS\" in Alamofire.xcworkspace<br \/>\n*** Building scheme \"Alamofire OSX\" in Alamofire.xcworkspace<br \/>\n<\/code><\/p>\n<p>Once completed you should see a new file and new folder in your project directory: <code>Cartfile.resolved<\/code> and <code>Carthage<\/code>, respectively.<\/p>\n<p>You are going to want to check in both <code>Cartfile<\/code> and <code>Cartfile.resolved<\/code>, so add them to your repository.<br \/>\n<code><br \/>\ngit add Cartfile<br \/>\ngit add Cartfile.resolved<br \/>\n<\/code><\/p>\n<p>You do <i>not<\/i> need to add the <code>Carthage<\/code> folder, so you can add it to your <code>.gitignore<\/code> file if you like.<\/p>\n<p>Now, let&#8217;s add the build output from Carthage to our project. First, go to your project&#8217;s <b>General<\/b> tab and locate the section <b>Linked Frameworks and Libraries<\/b>. You can either drag-and-drop the built framework from the Finder, or use the <b>+<\/b> button and navigate to the folder in Xcode. <\/p>\n<p><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2015\/02\/Add_Alamofire.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2015\/02\/Add_Alamofire.png\" alt=\"Add_Alamofire\" width=\"831\" height=\"198\" class=\"alignnone size-full wp-image-1263\" \/><\/a><\/p>\n<p>You should see the framework added to the list:<\/p>\n<p><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2015\/02\/Alamofire_added.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2015\/02\/Alamofire_added.png\" alt=\"Alamofire_added\" width=\"804\" height=\"155\" class=\"alignnone size-full wp-image-1264\" \/><\/a><\/p>\n<p>Now, go to Build Phases and create a new <b>Run Script<\/b> phase. The script contents will be:<\/p>\n<pre>\r\n\/usr\/local\/bin\/carthage copy-frameworks\r\n<\/pre>\n<p>Then add an <b>Input File<\/b> as the framework you want to copy, i.e., <code>Alamofire.framework<\/code>.<br \/>\n<a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2015\/02\/Carthage_RunScript.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2015\/02\/Carthage_RunScript.png\" alt=\"Carthage_RunScript\" width=\"887\" height=\"396\" class=\"alignnone size-full wp-image-1265\" \/><\/a><\/p>\n<p>That&#8217;s it!  With your Xcode project set up to pull in the built framework that Carthage created, you&#8217;re ready to build and run!  Much easier than using than CocoaPods or git submodules.<\/p>\n<h2>Gotchas<\/h2>\n<p>If you are iOS developer that likes to stay on the bleeding edge you undoubtedly have both the release and beta versions of Xcode installed on your Mac. If you build your projects solely within Xcode there&#8217;s typically no conflict between the two, but since Carthage builds your frameworks from the command-line, the version of Xcode selected by <code>xcodebuild<\/code> matters. For example, if your <code>xcodebuild<\/code> is set to use the release version of Xcode and you run <code>carthage update<\/code> and then include that framework in an Xcode beta project, you will likely get the error <code>Module file was created by an older version<\/code>. To resolve this use <code>xcode-select<\/code> to switch <code>xcodebuild<\/code> to use the beta version of Xcode:<\/p>\n<pre>\r\nsudo xcode-select -s \/Applications\/Xcode-Beta.app\r\n<\/pre>\n<p>As of February 28, 2015, the Alamofire main branch hasn&#8217;t been updated to support Xcode 6.3 (and by extension, Swift 1.2).  To continue working with Alamofire in your Xcode 6.3 projects, update your <code>Cartfile<\/code> to specify a <i>branch dependency<\/i>:<\/p>\n<pre>\r\ngithub \"Alamofire\/Alamofire\" \"xcode-6.3\"\r\n<\/pre>\n<p>Run <code>carthage update<\/code> and Carthage will checkout the <code>xcode-6.3<\/code> branch of Alamofire and compile it.  If you get a build failure make sure you&#8217;re using the right <code>xcodebuild<\/code>!<br \/>\n<code><br \/>\n% xcodebuild -version<br \/>\nXcode 6.3<br \/>\nBuild version 6D520o<br \/>\n<\/code><\/p>\n<h2>Get the Code<\/h2>\n<p>You can check out our example application (for details on the application, see our post <a href=\"http:\/\/dev.iachieved.it\/iachievedit\/http-json-requests-with-swift-and-alamofire\">here<\/a>)of using Alamofire and Carthage on Bitbucket.  There are two branches, <code>carthage<\/code> and <code>carthage-xcode-6.3<\/code>. <\/p>\n<p>If you&#8217;re using Xcode 6.1 then you will want to use the <code>carthage<\/code> branch:<\/p>\n<pre>\r\ngit clone git@bitbucket.org:joeiachievedit\/alamofire-translator translator\r\ncd translator\r\ngit checkout carthage\r\nsudo xcode-select -s \/Applications\/Xcode.app\r\ncarthage update\r\n<\/pre>\n<p>If you&#8217;re using Xcode 6.3 (which is beta at the time of this writing):<\/p>\n<pre>\r\ngit clone git@bitbucket.org:joeiachievedit\/alamofire-translator translator\r\ncd translator\r\ngit checkout carthage-xcode-6.3\r\nsudo xcode-select -s \/Applications\/Xcode-Beta.app\r\ncarthage update\r\n<\/pre>\n<p><b>Note!<\/b>  If you open the translator project and try to build and are met with <code>No such module 'Alamofire'<\/code> you didn&#8217;t build Alamofire with <code>carthage update<\/code> first!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have never been a fan of CocoaPods due to its intrusiveness in the build process and insistence on creating Xcode project workspaces. Unfortunately managing dependencies through the use of Git submodules is no better. How many times have you told yourself, &#8220;Okay, I understand submodules now&#8221; only to be met with fatal: reference is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,2],"tags":[],"class_list":["post-1230","post","type-post","status-publish","format-standard","hentry","category-apple","category-xcodetips"],"_links":{"self":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/1230"}],"collection":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/comments?post=1230"}],"version-history":[{"count":42,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/1230\/revisions"}],"predecessor-version":[{"id":1979,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/1230\/revisions\/1979"}],"wp:attachment":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media?parent=1230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/categories?post=1230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/tags?post=1230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}