Search code examples
iosobjective-cframeworksincludephotosframework

How do you #include frameworks at run time


I have an app in the App Store whose minimum supported version is iOS 7.1.

For the next version, I've enhanced it to use the Photos framework, which was introduced in iOS 8.

In the code for the next version of the app, I've ensured that if it's running on an iOS 7 device, the new functionality is hidden.

However, when I try and run the app on my iOS 7.1 test device, it fails because I've included the Photos framework in a number of the classes, using:

#import <Photos/Photos.h>

The error I receive in Xcode is:

"dyld: Library not loaded: /System/Library/Frameworks/Photos.framework/Photos Referenced from: /var/mobile/Applications/2CA13C9B-EABC-47C3-A198-A7C703EACD59/ABCapp.app/ABCapp Reason: image not found"

Is there any way to do this at run time rather than compile time to ensure that I can still support iOS 7?

Thanks.


Solution

  • Use framework weak linking.

    When a symbol in a framework is defined as weakly linked, the symbol does not have to be present at runtime for a process to continue running. The static linker identifies a weakly linked symbol as such in any code module that references the symbol. The dynamic linker uses this same information at runtime to determine whether a process can continue running. If a weakly linked symbol is not present in the framework, the code module can continue to run as long as it does not reference the symbol.

    And here's Marco Arment take on weak linking:

    If you wanted your iPhone or iPad app to work with older versions of the OS, or if you wanted to make a universal app that ran on both iPhone and iPad, you need to ensure that the code never tries to call a method or instantiate an object that doesn’t exist on its OS. [...] The other option to avoid all of that is weak-linking, which makes the runtime manually look up the existence of every symbol before its first use.