我有一个第三方 API,我正试图将其集成到我的 iOS 通用应用程序中。如果在设备上运行,API 工作正常,但在模拟器上运行时抛出编译时链接错误。那么,如果我在模拟器上运行,有没有一种方法可以跳过他们的静态库链接?
谢谢
最佳答案
实际上,这比我想象的要容易得多。
第 1 步:将链接器标志 -ObjC
和 -all_load
添加到您的目标。这告诉 objc 运行时,即使我们不在代码中引用某个类,它仍会将其加载到内存中。
第 2 步:在您的代码中,您可以这样做:
Class cls = NSClassFromString(@"SomeClassInStaticLibrary");
if (cls == nil)
{
// on the simulator
}
else
{
// on the device, use the class like usual
id myInstance = [[cls alloc] init];
}
不幸的是,您必须将所有内容都作为 id
来引用,因为如果您包含 header ,则会出现链接器错误。
虽然有点乱,但确实有效。
关于iphone - 在 Xcode 中有条件地链接第三方 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11040553/