Swift,iOS 8+框架

3
我想创建一个共享的Swift库,也就是Cocoa Touch Framework,用于在我的两个iOS应用程序之间共享。我不是全职iOS开发人员,所以对XCode等方面的知识有很多空白。
我找到了这篇文章Creating your first iOS Framework,以为我已经成功了:-)。可惜,我无法让它工作,主要是因为我不知道自己在做什么。
我只想创建一个(私有的)框架(库),用于共享Swift代码。我想在框架中使用CocoaPods引入其他框架,如Alamofire。我没有意图将此框架公开。
我几乎不理解为什么会使用Carthage(而不是CocoaPods)来管理框架并将其导入到消费应用程序中的机制。但是,该示例混合使用了Carthage和CocoaPods,并设置了一个Podspec,我认为我不需要,这让我感到困惑。
在这篇文章中,子项目的概念以及将下载的Carthage框架拖入框架并在使用应用程序中“导入”共享框架本身都让我感到困惑。我想知道为什么要这样做以及其后果是什么。
那么,我是不是走错了路?有没有一个非常简单的方法可以在两个相关应用程序之间私下共享Swift代码和其他工件呢?
有人能解释一下理论吗?
我很少有时间来处理我的应用程序,当我想到重复编码时,我的内心就会感到不安,所以我浪费了无数个小时来寻找那个简单的指南,并尝试遵循我找到的指南 - 最终都是令人失望的结果。
任何帮助都将不胜感激。
谢谢, 彼得...
1个回答

3
我建议您使用Carthage,因为它更简单,不会对您的项目文件施加任何限制。我无法帮助您使用CocoaPods,因为我从未使用过它,我将指导您完成使用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
    

您的框架应该已经设置好可以与Carthage一起使用了。您可以按照这里的步骤将框架添加到您的iOS项目中:

  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
    

现在应该可以工作了,如果你遇到了什么问题,请告诉我。

编辑:当你对库进行更改时,需要执行以下操作:

  1. 提交变更
  2. git tag 0.2 -m "Version 0.2"

在使用框架的项目中,只需运行 carthage update --platform iOS,无须其他操作。

如果你的框架也需要使用 Alamofire,则需要和其他项目一样设置Cartfile。当运行carthage update时,Carthage将解决每个框架所需的所有子框架,以及它们的子框架等。

如果一个Cartfile想要"Alamofire" ~> "1.0",而另一个"Alamofire" ~> "2.0",则可能会发生冲突,但这种情况很少发生。


非常感谢您抽出时间来做这件事。我还没有尝试过,但我有几个问题。我主要是对开发生命周期感到困惑。也就是说,当我对库进行更改时会发生什么?它是否需要再次使用carthage build命令构建?标签是否必须更改以便消费者识别更改?此外,您能否解释一下在消费者和库中都使用的第三方库会发生什么?我确定所有人都需要Alamofire。会有冲突吗? - psparago
这个回答非常清晰和有帮助!!完美运行。 - Alex

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接