Swift, iOS 8+ Framework

2019-04-11 18:17发布

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...

1条回答
虎瘦雄心在
2楼-- · 2019-04-11 19:03

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:

  1. If you haven't already, install Homebrew, it's the most commonly used package manager on OSX. Command:

    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    
  2. Install Carthage. Command:

    brew update && brew install carthage
    
  3. Create a new Project, using the iOS Cocoa Touch Framework template. When saving, check "Create Git repository".

  4. Add a .gitignore file to your project folder. If you're in the project directory, you can do this:

    curl https://raw.githubusercontent.com/github/gitignore/master/Swift.gitignore > .gitignore
    
  5. Add the code you want to the project, remember that only public declarations are available outside the framework.

  6. Edit the scheme (Shortcut ⌘<) and check Shared. This is required for Carthage to work.

  7. Commit the changes (Shortcut ⌥⌘C), check every file (Rightclick > Check All) and enter a commit message (e.g. "First version")

  8. Tag the release (required by Carthage): Command (In the project directory):

    git tag 0.1 -m "Version 0.1"
    
  9. Run the following command to verify and build the project via Carthage:

    carthage build --no-skip-current
    

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:

  1. Add a Cartfile to your project (simply a file named Carthage (no extension)), you can use Xcode for this. The file should contain this:

    git "file:///path/to/your/framework/project/directory"
    github "Alamofire/Alamofire"
    # Other frameworks
    
  2. Run

    carthage update --platform iOS
    
  3. Open the Carthage/Build/iOS folder (in Finder) and drag-and-drop every .framework file to the Linked Frameworks and Libraries section in the project settings (General tab).

  4. Add a new Run Script phase (Project Settings > Build Phases > "+" button). Put the line

    /usr/local/bin/carthage copy-frameworks
    

    and add every framework to the "Input Files" section:

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

Now this should work, let me know if you're stuck somewhere.

EDIT: When you make changes to the library, you do this:

  1. Commit
  2. 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 running carthage 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.

查看更多
登录 后发表回答