I want to create a shared Swift library a.k.a. Cocoa Touch Framework to share between two of my iOS applications. I'm not a full-time iOS developer, so I have many gaps in my knowledge of XCode etc.
I found this article Creating your first iOS Framework and thought I was home free :-). Sadly, I can't get it working, mostly because I don't know what I'm trying to do.
I simply want to create a (private) framework (library) of shared Swift code between my two applications. I'd like to use CocoaPods in the framework to bring in other frameworks such as Alamofire. I have no intention of making this framework public.
I barely understand why one would use Carthage (vs. CocoaPods) as the mechanism to manage the framework and import it into the consuming apps. But, the example mixes Carthage and CocoaPods and sets up a Podspec which I don't think I want and confuses me.
In the article, the notion of sub-projects in general, and dragging the Carthage downloaded frameworks into the framework and "importing" the shared framework itself in the consuming apps all confuses the heck out of me. I want to know why I'm doing that and what the ramifications of it are.
So, am I going about this the wrong way? Does anyone have a brain dead simple recipe for sharing Swift code and other artifacts privately between two related applications?
Can anyone explain the theory?
I get very little time to work on my apps and my flesh crawls when I think about duplicating code, so I've wasted countless hours trying to find that simple guide and more hours attempting to follow those I have found - all to disappointing ends.
Any help would be most appreciated.
Thanks, Peter...
I suggest you to use Carthage, because it's much simpler and doesn't impose any constraints on your project file. I cannot help you with CocoaPods as I have never used it, I'll guide you through the steps required with Carthage:
If you haven't already, install Homebrew, it's the most commonly used package manager on OSX. Command:
Install Carthage. Command:
Create a new Project, using the iOS Cocoa Touch Framework template. When saving, check "Create Git repository".
Add a
.gitignore
file to your project folder. If you're in the project directory, you can do this:Add the code you want to the project, remember that only
public
declarations are available outside the framework.Edit the scheme (Shortcut ⌘<) and check
Shared
. This is required for Carthage to work.Commit the changes (Shortcut ⌥⌘C), check every file (Rightclick > Check All) and enter a commit message (e.g. "First version")
Tag the release (required by Carthage): Command (In the project directory):
Run the following command to verify and build the project via Carthage:
Your framework should be set up to work with Carthage now. You can follow the steps here to add the framework to your iOS project:
Add a Cartfile to your project (simply a file named
Carthage
(no extension)), you can use Xcode for this. The file should contain this:Run
Open the
Carthage/Build/iOS
folder (in Finder) and drag-and-drop every.framework
file to theLinked Frameworks and Libraries
section in the project settings (General tab).Add a new
Run Script
phase (Project Settings > Build Phases > "+" button). Put the lineand add every framework to the "Input Files" section:
Now this should work, let me know if you're stuck somewhere.
EDIT: When you make changes to the library, you do this:
git tag 0.2 -m "Version 0.2"
In your project that's using the framework, just run
carthage update --platform iOS
, nothing else to do.If your Framework needs to use Alamofire as well, it needs the same
Cartfile
setup as the other project. When runningcarthage update
, Carthage will resolve all subframeworks needed for every framework, and their subframeworks, etc.There can be conflicts if e.g. one
Cartfile
wants"Alamofire" ~> "1.0"
and the other"Alamofire" ~> "2.0"
, but that rarely happens.