简体   繁体   English

Swift,iOS 8+框架

[英]Swift, iOS 8+ Framework

I want to create a shared Swift library aka Cocoa Touch Framework to share between two of my iOS applications. 我想创建一个共享的Swift库,也就是Cocoa Touch Framework,以便在我的两个iOS应用程序之间共享。 I'm not a full-time iOS developer, so I have many gaps in my knowledge of XCode etc. 我不是全职的iOS开发人员,所以我对XCode等知识有很多差距。

I found this article Creating your first iOS Framework and thought I was home free :-). 我发现这篇文章创建你的第一个iOS框架,并认为我在家免费:-)。 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. 我只想在我的两个应用程序之间创建共享Swift代码的(私有)框架(库)。 I'd like to use CocoaPods in the framework to bring in other frameworks such as Alamofire. 我想在框架中使用CocoaPods引入其他框架,如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. 我几乎不明白为什么会使用Carthage(与CocoaPods)作为管理框架并将其导入消费应用程序的机制。 But, the example mixes Carthage and CocoaPods and sets up a Podspec which I don't think I want and confuses me. 但是,这个例子混合了Carthage和CocoaPods并建立了一个我不认为我想要的Podspec并让我感到困惑。

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. 在文章中,一般来说子项目的概念,将Carthage下载的框架拖入框架并在消费应用程序中“导入”共享框架本身都让我感到困惑。 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? 有没有人在两个相关的应用程序之间私有地共享Swift代码和其他工件?

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. 我建议你使用Carthage,因为它更简单,并且不会对你的项目文件施加任何限制。 I cannot help you with CocoaPods as I have never used it, I'll guide you through the steps required with Carthage: 我无法帮助您使用CocoaPods,因为我从未使用它,我将引导您完成迦太基所需的步骤:

  1. If you haven't already, install Homebrew , it's the most commonly used package manager on OSX. 如果你还没有,安装Homebrew ,它是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. 使用iOS Cocoa Touch Framework模板创建一个新项目。 When saving, check "Create Git repository". 保存时,选中“Create Git repository”。

  4. Add a .gitignore file to your project folder. .gitignore文件添加到项目文件夹中。 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. 将您想要的代码添加到项目中,请记住在框架外只能使用public声明。

  6. Edit the scheme (Shortcut ⌘<) and check Shared . 编辑方案(快捷键⌘<)并选中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 (eg "First version") 提交更改(快捷方式⌥⌘C),检查每个文件(右键单击>全部检查)并输入提交消息(例如“第一版”)

  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验证和构建项目:

     carthage build --no-skip-current 

Your framework should be set up to work with Carthage now. 您的框架应该立即与Carthage合作。 You can follow the steps here to add the framework to your iOS project: 您可以按照此处的步骤将框架添加到iOS项目中:

  1. Add a Cartfile to your project (simply a file named Carthage (no extension)), you can use Xcode for this. Cartfile添加到项目中(只是一个名为Carthage (无扩展名)的文件),您可以使用Xcode。 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). 打开Carthage/Build/iOS文件夹(在Finder中)并将每个.framework文件拖放到项目设置(常规选项卡)中的Linked Frameworks and Libraries部分。

  4. Add a new Run Script phase (Project Settings > Build Phases > "+" button). 添加新的Run Script阶段(“项目设置”>“构建阶段”>“+”按钮)。 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. 在你正在使用框架的项目中,只需运行carthage update --platform iOS ,别无其他。

If your Framework needs to use Alamofire as well, it needs the same Cartfile setup as the other project. 如果您的Framework也需要使用Alamofire,它需要与其他项目相同的Cartfile设置。 When running carthage update , Carthage will resolve all subframeworks needed for every framework, and their subframeworks, etc. 运行carthage update ,Carthage将解析每个框架及其子框架等所需的所有子框架。

There can be conflicts if eg one Cartfile wants "Alamofire" ~> "1.0" and the other "Alamofire" ~> "2.0" , but that rarely happens. 如果一个Cartfile想要"Alamofire" ~> "1.0"而另一个"Alamofire" ~> "2.0" ,则可能存在冲突,但这种情况很少发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM