3

I know of familiar tutorials on this, but introduction of framework XCode 6 template has changed the game.

I already watched WWDC 2014 video about building modern frameworks but it talks more about building extensions, framework & app all inside single project. It does not specify if the framework I make with it is truly reusable across any project.

I am building framework the XCode 6 way (File->New Project->Framework and Library->Cocoa Touch Framework), but when I import it inside my test app project (separate from framework project) - I keep getting various errors.

Example: Include of non-modular header inside framework, and so on.

I know this is not what it says, and there are quite some missing steps in whatever I am doing. The older tricks may have worked for everyone, but I simply don't find which way to follow after XCode 6.

For example, there is some folder structure that a framework needs, but XCode 6 doesn't comply to it while building it. Is it right? If not, how can I change the way the XCode builds framework folder hierarchy?

Do I go back to old school or am I screwing some tiny thing in XCode 6 that I am unable to create a reusable framework?

yf526
  • 113
  • 7
Nirav Bhatt
  • 6,940
  • 5
  • 45
  • 89
  • Created a framework along with nibs using a bundle. However nibs are unable to load images from xcassets. How could one force nibs to load images from its own xcasset (xcassets are not part of bundle or framework - not even if you want to make it to by copy bundle resources stage) – Nirav Bhatt Jun 10 '15 at 08:48

1 Answers1

2

I am not sure if you are trying to build a framework with Objective-C or Swift as your question doesn't state it. I've encountered errors you are mentioning with Swift so I'll give you my method to build Swift frameworks.

I found the process for Objective-C to be very straightforward and well documented, so I'll skip this.

As for Swift, there are a few things to consider. First, Swift static libraries are not supported, so you must exclusively use a framework (aka dynamic library) when linking an app to a library.

Here are the steps:

  1. Create the Framework using New > Project under IOS > Framework & Library, select Cocoa Touch Framework

  2. To avoid the "ld: warning: directory not found for option..." goto Library Search Paths in Build Settings for your target and delete the paths.

  3. You can't mix Objective-C with Swift so don't even consider adding the Swift-Header bridge file in your code.

  4. There are some cases in swift where you need to import code from unexposed Frameworks. I've successfully used the module-map inside the framework to deal with these case.

  5. I also select CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES = YES in the Build Settings to solve 'include of non-modular header inside framework module'. That seems to work

  6. I make sure that the header file that gets generated is marked as Public (not Project). Click on the file and you'll see the selection in the inspector under 'Target Membership'

You may run into some bizarre error messages when building. Xcode has a tendency to report linker errors when your code can't compile correctly resulting in missing files the linker needs to output its binaries. Sometimes XCode won't show the errors in the files you are compiling and you need to go manually on the build output and go back to the files. Some other time, you'll get a problem where you need to delete the cache. Those issues I call XCode blues and deal with it constantly. I found this type of problems happens more often when building libraries. The rest should work as expected.

John Difool
  • 5,572
  • 5
  • 45
  • 80
  • +1 thanks for the library search path - it worked. I am building with objective c. After building the folder structure as stated by Apple I got rid of the older errors. But now my client app is unable to find the object file for the viewcontroller I am exporting from the framework. I don't see it built as part of framework. All I find is header and nib. Any idea? – Nirav Bhatt Jun 04 '15 at 06:09
  • Did you add the files to the Compile Sources section for the specific target? Make sure you click on the target and double check. Also, make sure you include the binary in the target app. Go into project settings, select the general tab of the target. Add the framework in the "Embedded Binaries" section – John Difool Jun 04 '15 at 06:36
  • It's done. But I suppose there is some output file missing inside framework bundle. I don't know why it wouldn't include Viewcontroller.o when Viewcontroller.h is one of the public header. – Nirav Bhatt Jun 04 '15 at 06:39
  • Oops. I edited while you were replying. Check the first sentence in my previous comment. – John Difool Jun 04 '15 at 06:41
  • yes, that was my first check too - the viewcontroller.m is in the list of files to compile. – Nirav Bhatt Jun 04 '15 at 06:51
  • I am feeling that what XCode 6 doesn't do is write executable code inside the framework by itself. The first tut I mentioned - it builds framework on top of static library - something that I should have done in the first place, instead of relying on XCode's built-in template for framework project. I think I am on track now. – Nirav Bhatt Jun 04 '15 at 07:03