Saturday, October 22, 2016

XCode Objective-C custom frameworks and iOS 10


There are two kinds of frameworks, those that come with XCode and those that you create on your own. Those that come with XCode can be used without any special steps, albeit the headers, but the other ones need special configuration, and there are two ways to it:
First, Drag the framework package, e.g. somelib.framework, to the Frameworks folder of the project and choose to copy the framework to the project and you are done. The problem with this approach is that the framework is "copied" to the project, so any changes that you may do to the framework in its own project, and new builds would not be reflected immediately and automatically in the dependent project. You will have to copy again and again for every change that you make.


Second, Add the framework package by drag 'n' drop, but do not copy. You can access the framework in XCode, but the linker will throw an error, stating the framework is missing. It's because the XCode does not take an additional step that it definitely should, and that is to add the framework's path to Framework Search Paths automatically, which you will have to do manually.

To add the path, select the target and go to "Build Settings." Scroll down to find the section labeled  "Search Paths". Under that you will find "Framework Search Paths". When you double click on the blank space on the right, it will open up a list with (+) and (-) buttons. Add the (+) button and add the path to the folder in which the framework is located, and you are good to go. Now the linker will link without throwing an error.

For iOS 8 and later, there would be a runtime error about missing library, stating, "dyld: Library not loaded ... Reason: image not found." This can be fixed by selecting the target and going to "General." After scrolling down, you should see a section labeled "Embedded Binaries." Press the (+) sign, and add the framework in question, and your app should not crash. That's it.

No comments: