iOS 静态与动态框架说明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27899799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS Static vs Dynamic frameworks clarifications
提问by csotiriou
I have to admit that with the release of iOS 8 I am a bit confused about dynamic and static frameworks in iOS.
我不得不承认,随着 iOS 8 的发布,我对 iOS 中的动态和静态框架有点困惑。
I am looking for a way to distribute a library that I created, and I need to support iOS 7 and above. (Note: This will be a proprietary framework. I cannot use cocoa pods, and I also cannot distribute the source). Here is what I already know:
我正在寻找一种分发我创建的库的方法,我需要支持 iOS 7 及更高版本。(注意:这将是一个专有框架。我不能使用可可豆荚,也不能分发源代码)。这是我已经知道的:
- iOS 8 introduced "embedded frameworks" for iOS, but, as I understand, they do not work for iOS 7, only for iOS 8 and above.
- I have the option of distributing my library as a static library (.a file) and also distribute the headers. I know that this is a common way of dealing with the situation, but I would like to find something simpler than that (and also to bundle some resources with it, if possible).
- I have also found that iOS 7 does not support dynamic .framework libraries (only static) because it doesn't support dynamic linking. But iOS 8 does, as well as static linking.
- iOS 8 为 iOS 引入了“嵌入式框架”,但据我所知,它们不适用于 iOS 7,仅适用于 iOS 8 及更高版本。
- 我可以选择将我的库作为静态库(.a 文件)分发,也可以分发标头。我知道这是处理这种情况的常用方法,但我想找到比这更简单的方法(如果可能,还可以捆绑一些资源)。
- 我还发现 iOS 7 不支持动态 .framework 库(仅静态),因为它不支持动态链接。但 iOS 8 和静态链接一样。
And here are my questions regarding this information:
以下是我对这些信息的问题:
- I saw that I can create a .framework target, and make it static, by changing the Mach-O type to "static library. Would that be enough in order to support iOS 7 without any problems, and also to distribute my library as a .framework bundle? If so, why is "embedded frameworks" in iOS 8 that big of a deal, as many resources on the internet are suggesting? Am I missing something?
- Is it necessary to codesign the .framework just as I do with any other application I make?
- What if I need to include other resources (like Core Data or Images) with my .framework file? Will I need to make a separate .bundle file for that?
- 我看到我可以创建一个 .framework 目标,并通过将 Mach-O 类型更改为“静态库”来使其成为静态目标。这是否足以支持 iOS 7 没有任何问题,并将我的库作为.framework 包?如果是这样,为什么 iOS 8 中的“嵌入式框架”如此重要,正如互联网上的许多资源所暗示的那样?我错过了什么吗?
- 是否有必要像我制作的任何其他应用程序一样对 .framework 进行协同设计?
- 如果我需要在我的 .framework 文件中包含其他资源(如 Core Data 或 Images)怎么办?我需要为此制作一个单独的 .bundle 文件吗?
采纳答案by Subbu
Before iOS8, Xcode only allowed the option of creating static libraries for iOS. The common problem with that was we had to ship the binary and headers separately.
在 iOS8 之前,Xcode 只允许为 iOS 创建静态库的选项。常见的问题是我们必须分别发送二进制文件和头文件。
Later, some developers came with the idea of creating 'static frameworks'. [the .framework is just a folder with symbolic links to the lib and the headers]. One such example is https://github.com/jverkoey/iOS-Framework
后来,一些开发人员提出了创建“静态框架”的想法。[.framework 只是一个带有指向 lib 和头文件的符号链接的文件夹]。一个这样的例子是https://github.com/jverkoey/iOS-Framework
This option will work for iOS 7 or 8 or before that. Because they are just static libraries with the convenience of bundling the headers files along.
此选项适用于 iOS 7 或 8 或更早版本。因为它们只是静态库,可以方便地捆绑头文件。
As for your questions of the resources, we would need to bundle them in '.bundle'.. For shipping them i am not sure if we can enclose them in the .framework folder.. In the past i used to ship my libs as a static framework and bundle...
至于您对资源的问题,我们需要将它们捆绑在“.bundle”中。为了运送它们,我不确定我们是否可以将它们包含在 .framework 文件夹中。过去我曾经将我的库作为一个静态框架和捆绑...
However the above option will not work for you if you use Swift. Xcode does not support building static libraries that include swift code.
但是,如果您使用 Swift,则上述选项对您不起作用。Xcode 不支持构建包含 swift 代码的静态库。
You must go with Dynamic frameworks if there is swift usage. In theory, Dynamic frameworks work in iOS7.. But, i think iTunes Connect will reject if the app is targeting iOS7 and uses Dynamic frameworks :-).
如果快速使用,您必须使用动态框架。理论上,动态框架在 iOS7 中有效。但是,我认为如果应用程序针对 iOS7 并使用动态框架,iTunes Connect 会拒绝:-)。
Hope this helps
希望这可以帮助
回答by Avijeet Dutta
With Xcode 9 onwards you can create static frameworks for Swift as well. This is possible due to ABI source compatibility. All you need to do is just change the Mach-O type
under build settings of the framework target.
This technique is also applicable to Hybrid Frameworks(frameworks with Swift and Objective-C code).
从 Xcode 9 开始,您也可以为 Swift 创建静态框架。由于 ABI 源兼容性,这是可能的。您需要做的就是更改Mach-O type
框架目标的下构建设置。这种技术也适用于混合框架(带有 Swift 和 Objective-C 代码的框架)。
回答by AliasCocoa
Swift does not work in static lib. If you have to use dynamic framework, you have to set min iOS to 8.0 becauseAppStore reject ios 7 with dynamic framework
Swift 在静态库中不起作用。如果必须使用动态框架,则必须将最低 iOS 设置为 8.0,因为AppStore 拒绝使用动态框架的 ios 7
回答by Beau Nouvelle
I don't have all the answers but I'll try to address some of your questions here.
我没有所有的答案,但我会尝试在这里解决您的一些问题。
You will get a warning for using these frameworks in iOS 7, however that's all it is, a warning. See this answer.
You can include other resources like CoreData however you'll need to create them in code manually. Here's a tutorialshowing how to create a core data model.
You have to code sign dynamic libraries for iOS.
You need to make sure your framework supports both simulator and device architectures if you're planning to distribute it.
回答by yoAlex5
static
or dynamic
in name usually points into a Linking
[About]type
static
or dynamic
in name 通常指向Linking
[About]类型
Frameworks can be static
or dynamic
[Check static or dynamic]
框架可以是static
或dynamic
[检查静态或动态]
You can change the format of library that will have an impact on a Linker
by changing Framework target -> Build Settings -> Mach-O Type
[About]to Static Library
or Dynamic Library
. By default Xcode has Dynamic Library
value.
您可以Linker
通过将Framework target -> Build Settings -> Mach-O Type
[About]更改为Static Library
或来更改将对 a 产生影响的库的格式Dynamic Library
。默认情况下,Xcode 具有Dynamic Library
价值。
Depends on this setting different types of binary will be generated
取决于此设置,将生成不同类型的二进制文件
After you successfully configure a consumer[Link vs Embed]
成功配置消费者后[Link vs Embed]
Static Linker
ld: at compiletime will include all code from the static library
into the executable object file.
Static Linker
ld:在编译时将包含所有代码从static library
到可执行目标文件中。
Dynamic Linker
dyld: at load/run time will try to find the embedded framework using @rpath
[About]and link it
Dynamic Linker
dyld:在加载/运行时将尝试使用@rpath
[About]查找嵌入式框架并链接它