您如何在 运行 时间#include 框架

How do you #include frameworks at run time

我在 App Store 中有一个应用程序,其支持的最低版本为 iOS 7.1.

对于下一个版本,我对其进行了增强以使用 iOS 8.

中引入的照片框架

在下一版应用程序的代码中,我确保如果它 运行 在 iOS 7 设备上运行,新功能将被隐藏。

但是,当我尝试 运行 我的 iOS 7.1 测试设备上的应用程序时,它失败了,因为我在许多 类 中包含了照片框架,使用:

#import <Photos/Photos.h>

我在 Xcode 中收到的错误是:

"dyld:未加载库:/System/Library/Frameworks/Photos。framework/Photos 引用自:/var/mobile/Applications/2CA13C9B-EABC-47C3-A198-A7C703EACD59/ABCapp.app/ABCapp 原因:找不到图像

有什么方法可以在 运行 时间而不是编译时间执行此操作以确保我仍然可以支持 iOS 7?

谢谢。

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

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.