116

I have to admit that with the release of iOS 8 I am a bit confused about dynamic and static frameworks in 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 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.

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?
Elrond_EGLDer
  • 47,430
  • 25
  • 189
  • 180
csotiriou
  • 4,691
  • 4
  • 33
  • 42
  • 1
    "iOS 7 does not support .framework libraries because it doesn't support dynamic linking" This statement is not correct. – Midhun MP Jan 12 '15 at 10:46
  • 1
    I see. can you tell me the correct sentence? Is it false because iOS 7 supports dynamic linking or because iOS 7 supports .framework libraries? Or both? – csotiriou Jan 12 '15 at 10:50
  • 1
    Remeber AVFoundation and CoreGraphics all are .framework. Probably this help you to find the answers for all your questions: http://www.raywenderlich.com/65964/create-a-framework-for-ios – Midhun MP Jan 12 '15 at 10:58
  • I fixed the sentence. I have already read the link, before I posted my questions. This link does not explain anything regarding my questions. It starts from a static library and wraps it into a .framework file MANUALLY. I am talking about a Cocoa Touch Framework target in Xcode 6 and then changing the type into a static library. It doesn't mention code signing (why and if it's necessary), additional resources, etc. – csotiriou Jan 12 '15 at 11:04
  • For additional resources you need to include a bundle – Midhun MP Jan 12 '15 at 11:16
  • 1
    You can use CocoaPods to distribute a proprietary framework (eg Parse) – Ric Santos May 04 '15 at 04:28
  • Some useful information in this video : https://www.youtube.com/watch?v=aUGAiuuZ89w – Feru Sep 16 '16 at 13:41

5 Answers5

69

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.

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

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.

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...

However the above option will not work for you if you use Swift. Xcode does not support building static libraries that include swift code.

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 :-).

Hope this helps

Subbu
  • 2,068
  • 11
  • 18
18

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).

Avijeet Dutta
  • 561
  • 1
  • 5
  • 7
13

Static vs Dynamic linking

static or dynamic in name usually points into a Linking[About] type

Frameworks can be static or dynamic[Check static or 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.

Depends on this setting different types of binary will be generated

After you successfully configure a consumer[Link vs Embed]

Static Linker ld: at compile time will include all code from the static library into the executable object file.

Dynamic Linker dyld: at load/run time will try to find the embedded framework using @rpath[About] and link it

[Vocabulary]

yoAlex5
  • 13,571
  • 5
  • 105
  • 98
5

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 tutorial showing 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.

Community
  • 1
  • 1
Beau Nouvelle
  • 6,331
  • 3
  • 34
  • 49
5

Swift does not work in static lib. If you have to use dynamic framework, you have to set min iOS to 8.0 because AppStore reject ios 7 with dynamic framework

T D Nguyen
  • 5,353
  • 4
  • 38
  • 57
AliasCocoa
  • 69
  • 1
  • 4