如何在包括 Swift 的 iOS 中创建开发框架? [英] How Do I Create a Development Framework In iOS Including Swift?

查看:18
本文介绍了如何在包括 Swift 的 iOS 中创建开发框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是创建一个包含 Swift 和 Objective-C 的 iOS 框架,我可以在我的开发项目中使用它.这个框架的本质是框架本身正在发展.因此,每次我使用此框架构建项目时(我将使用框架的项目称为使用"项目,因为没有更好的术语),我想确保框架本身被重新构建,这一点很重要.我希望这是一个框架,因为我有一些使用应用程序,我想在这些应用程序中使用相同的框架代码.今天我已经为此苦苦挣扎了很久,并且浪费了很多时间在至少在我看来应该更容易的事情上.所以,我将分享我的过程.

My goal in this was to create an iOS framework that incorporates both Swift and Objective-C that I could use in my development projects. The nature of this framework is that the framework itself is undergoing development. So, it was important that each time I build a project using this framework (I'll call projects using the framework "using" projects for lack of a better term), I wanted to make sure the framework itself was re-built. I wanted this to be a framework because I have a few using apps across which I want to use the same framework code. I have struggled with this for a good hunk of today, and wasted a lot of time on something that should have been, in my thoughts at least, easier. So, I'll share my process.

推荐答案

首先要观察(这肯定不是我第一次观察!)是你不能使用下面的静态库来做到这一点IOS.Xcode 不会让你在静态框架中使用 Swift 试试看.Xcode 会拒绝你的意愿!

The first thing to observe (which was certainly not my first observation!) is that you cannot do this using a static library under iOS. Xcode will not let you use Swift in a static framework Try it. Xcode will deny your wishes!

这是我最终完成的过程.我必须处理的两个主要问题是:(i)使 Xcode 链接到 using 项目中的框架而没有错误,以及(ii)在 using 项目中访问框架的标头.在苹果的开明观点中,这两个问题是分开的.注意讽刺.;).

Here's the process I ended up with. The two main issues I had to deal with were: (i) making Xcode link to the framework in the using project without errors, and (ii) getting access to the headers of the framework in the using project. In Apple's enlightened view these two issues are separate. Note the sarcasm. ;).

1) 使用 Xcode 创建一个 Cocoa Touch 框架.我相信这适用于 Xcode6 和 Xcode7.使用:

1) Create a Cocoa Touch Framework using Xcode. I believe this works with Xcode6 and Xcode7. Use:

文件 > 新建 > 项目 > iOS > 框架 &图书馆 > 可可触摸框架

File > New > Project > iOS > Framework & Library > Cocoa Touch Framework

我碰巧在使用 Xcode7.(不要不要制作一个Cocoa Touch 静态库——就像我上面所说的,Xcode 不会让你将 Swift 合并到静态库中.

I happen to be using Xcode7. (Do not make a Cocoa Touch Static Library-- like I said above, Xcode will not let you incorporate Swift into static libraries).

2) 对于您的 Swift 类,确保成员和函数是 public.我没有对此进行过实验,但似乎 public 属性对于框架用户可见的成员和函数是必需的.

2) With your Swift classes, make sure the members and functions are public. I've not experimented with this, but it seems that the public attribute is necessary for the members and functions to be visible to users of the framework.

3) 将您想要的任何 Swift 类(和 Objective-C)添加到您的框架中.

3) Add what ever Swift classes (and Objective-C) you want to your framework.

4) 关闭该框架项目.(同一个项目不能在Xcode中打开两次,接下来需要将框架合并到你使用的项目中).

4) Close that framework project. (The same project can't be open twice in Xcode, and you need to incorporate the framework into your using project next).

5) 在 Xcode 中打开您正在使用的项目.对我来说,这是一个现有的通用应用程序项目.您可能正在创建一个新的 using 项目.无论如何,在 Finder 中将框架项目的 .xcodeproj 文件拖到正在使用的项目中.

5) Open your using project in Xcode. For me this was an existing universal app project. You may be creating a new using project. In any event, drag the .xcodeproj file of your framework project, in the Finder, into your using project.

6) 在您使用的项目中,打开您的框架项目.并将框架文件拖到 Build Phases 中的 Embed Frameworks 中(当我第一次开始实验时,Build Phases 中不存在 Embed Frameworks 部分,我还不知道是什么魔法让它出现的!).

6) Inside of your using project, open your framework project. And drag the framework file into Embed Frameworks in Build Phases (the Embed Frameworks section wasn't present in Build Phases when I first started my experiments and I don't know yet what magic caused it to appear!).

到目前为止,这些步骤应该使您能够构建和链接,而无需实际集成库代码的使用.(我使用的是 https://github.com/RadiusNetworks/swift-framework-example用于我的一些测试).

These steps so far should enable you to build and link without actually yet integrating the usage of your library code. (I was using https://github.com/RadiusNetworks/swift-framework-example for some of my testing).

7) 现在是致命一击:在 Build Settings 下,搜索 Framework Search Paths.并添加:

7) Now for the coup de grace: Under Build Settings, search for Framework Search Paths. And add in:

${TARGET_BUILD_DIR}/YourFrameworkName.framework

(看来您不必将其标记为递归).

(It seems you do not have to have this marked as recursive).

8) 在您使用框架的 Swift 代码文件中,您需要在每个文件的顶部添加一个导入:

8) In your Swift code files using the framework, you need to add an import at the top of each file:

import YourFrameworkName

您现在应该可以使用新库进行构建和链接了!

You should now be able build and link using your new library!

9) 还有一个问题:确保您的框架的 Deployment Target 与您的目标项目相匹配.例如,如果您使用的项目是为 iOS7 构建的,请确保您的框架是为 iOS7 或更早版本构建的.

9) One more gotcha: Make sure your Deployment Target for your framework matches your destination project. E.g., if your using project builds for iOS7, make sure your framework builds for iOS7 or earlier.

10) 第二个问题 (10/23/15):我刚刚了解到我的框架必须有App-Swift.h"(我为此使用的名称)作为 Objective-C 生成的接口头文件构建设置中的名称.当我把这个(Objective-C Generated Interface Header)拿出来(试图解决另一个问题)时,我在 App-Swift.h 中遇到了一些有趣的问题.这些问题看起来像:找不到 NSObject 的接口声明"?

10) Second gotcha (10/23/15): I just learned that it is necessary for my framework to have "App-Swift.h" (the name I use for this) as the Objective-C Generated Interface Header name in Build Settings. When I took this (Objective-C Generated Interface Header) out (trying to fix another issue), I get serveral interesting issues coming up in App-Swift.h. These issues look something like: "Cannot find interface declaration for NSObject"?

11) 第三个问题 (10/29/15):当我尝试将我的第一个应用程序上传到使用此框架的 iTunes Connect 时,我收到了一个上传错误.错误读取:

11) Third gotcha (10/29/15): When I tried to upload my first app to iTunes Connect that makes use of this Framework, I got an uploading error. The error read:

错误 ITMS-90206:无效的捆绑包.捆绑包位于'Your.app/Frameworks/YourFramework.framework' 包含不允许的文件'框架'."

ERROR ITMS-90206: "Invalid Bundle. The bundle at 'Your.app/Frameworks/YourFramework.framework' contains disallowed file 'Frameworks'."

各种 SO 和其他帖子都遇到了这种错误,对我来说,诀窍是,对于 Framework 目标,在 Build Settings 中,将Embedded Content Contains Swift Code"设置为 NO.(我的应用构建设置已经将此标志设置为 NO).

Various SO and other posts have run into this kind of error, and the trick for me was, for the Framework target, in Build Settings, to set "Embedded Content Contains Swift Code" to NO. (My app Build Settings had this flag set to NO already).

其中大部分步骤已完成的示例项目位于 https://github.com/crspybits/CocoaTouchFramework.git

An example project with most of these steps completed is on https://github.com/crspybits/CocoaTouchFramework.git

这篇关于如何在包括 Swift 的 iOS 中创建开发框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆