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

查看:114
本文介绍了如何在包含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链接到使用项目中的框架而没有错误,以及(ii)访问使用项目中框架的头部。在Apple的开明观点中,这两个问题是分开的。请注意讽刺。 ;)。

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 Framework 。我相信这适用于Xcode6和Xcode7。使用:

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


文件>新建>项目> iOS>框架&库> Cocoa Touch
Framework

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中打开你的使用项目。对我来说,这是一个现有的通用应用项目。您可能正在创建一个新的使用项目。无论如何,将您的框架项目的.xcodeproj文件在Finder中拖动到您的使用项目中。

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)在您的使用项目中,打开您的框架项目。并在构建阶段将框架文件拖入嵌入框架(构建阶段中不存在嵌入框架部分我首先开始我的实验,但我还不知道是什么魔法导致它出现!)。

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-我的一些测试的示例

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)现在为了恩典:在构建设置,搜索框架搜索路径。并加入:

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)还有一个问题:确保您的框架的部署目标与目标项目匹配。例如,如果您的使用项目是为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(我使用的名称)作为Build Settings中的Objective-C Generated Interface Header名称。当我把这个(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'包含不允许的文件
'Frameworks'。

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

各种SO和其他帖子都遇到了这种错误对于我来说,对于框架目标,在构建设置中,将嵌入内容包含Swift代码设置为NO。 (我的应用程序Build Settings已将此标志设置为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天全站免登陆