26

I have a an Xcode project which produces a static library. My team plans all new development in Swift. It is not possible to add Swift files to the static library project. We are dropping support for iOS 7, so it is now possible to include frameworks in our iOS app. Therefore, I intend to convert my static library project to a framework project.

I have looked but I cannot find any tools or advice for how to perform this conversion. The static library is large (more than 100 .m files).

I'm hoping for a better answer than create a new parallel framework target. I've attempted this twice. The first time as a swift target, but I wasn't able to easily import all the Objective C files. Next, as an Objective C target, but there is no .pch anymore.

Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117

2 Answers2

13

To convert the static/dynamic linked framework from static linked library,

  1. Add a new cocoa touch framework as a TARGET in your existing static linked library project.
  2. In the Build Phases, adding all the .m, .mm, .c, .cpp, .metal, etc. into "\Build Phases\Compile Sources" phase of your static linked framework target.
  3. Put the headers that you want to exposed in to "\Build Phases\Headers".
  4. For dynamic linked framework, remember to check the Mach-O Type setting in your Build Settings. If you are going to use swift, you need to make sure the Mach-O type is set as dynamic library so that it will become a dynamic linked framework. For static linked framework, you'll need to set the Mach-O type as static library, but you cannot use swift in the converted static linked framework (only objective-c, objective-c++, C++, C, etc. are allowed).

Then for the app that wants to use this framework just need to include the headers as #import and add the framework into "Build Phases\Link Binary With Libraries" of your App Target. If the converted framework is dynamic linked framework, you will need to put it into "Embedded Binaries".

joseph
  • 557
  • 3
  • 8
  • Soon as I add a swift file to my new project I get 'Swift is not supported for static libraries' – lostintranslation Jul 04 '16 at 19:21
  • The proposed way is to convert "static linked library" to "static linked framework". While Swift does not support static library at this time, you can not just add swift in to the newly created "static" linked framework. – joseph Aug 22 '16 at 00:30
  • I'm confused on your answer above @joseph, you said "you can not just add swift in to the newly created "static" linked framework.", what is the point of the steps in your answer then if the result is still not compatible with Swift? Maybe you meant to say "can" instead of "can not"? Also, if this indeed works for Swift, can you put more detailed steps? I'm having the same issue and I can't seem to fully understand the steps above (I'm fairly new in iOS development) – Franco Dec 29 '16 at 23:55
  • 1
    Are you aware of a method that allows copying all the file from the Build Phases, Compile Sources section of the static lib (yes, the Copy command) but allows pasting them all into the Compile Sources section of the framework? – Alex Zavatone Dec 30 '16 at 21:23
  • Franco, The static linked library is for objectie-c or objective-c++ project. If you need to add swift, you will need to make the Macho-O type to "Dynamic Library". – joseph Jan 25 '17 at 21:49
2

I saw that someone created the framework manually, creating a module.framework file and copying all the header files in a module.framework/Headers folder. This solution seems to work, the project can import correctly the files and see them as a framework correctly.

I'm not sure this is the best way to do it tough, I'm trying it on a big project that ATM is importing the static library through cocoapods, but it seems like I have some problem with the visibility of some of the classes using the framework.

Andrea
  • 219
  • 3
  • 9
  • can i include header files from other frameworks to my own framework so that end user will see my framework and can access classes from other's framework from mine? – Saty Mar 23 '16 at 09:14
  • Do you remember how he pasted them all in to the Compile Sources section of the framework? I've got a few thousand files I need to handle. – Alex Zavatone Dec 30 '16 at 21:24