将第三方框架添加到 Xcode 子项目或 iOS app 中的主项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23939346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:57:53  来源:igfitidea点击:

Add third party framework to Xcode subproject or main project in iOS app

iosxcodeios-frameworkssubproject

提问by indiantroy

My Xcode project "MyMainApp" has a static library type Xcode subproject called "MyLib". Now I need to use some third party iOS frameworks in my iOS app. I want to add/link them against only MyLib and not against MyMainApp so I can make the MyLib reusable and self-contained to use it in my other iOS apps too. A few view controllers in MyMainApp use one of the view controller that is a part of MyLib. This view controller in MyLib uses functionality from the classes that reside inside third party framework. Now the problem I am facing is if I add the third party frameworks to just MyLib and don't add them to MyMainApp, I get a linker error "Lexical or Preprocessor issue. XXX.h file not found". Everything works well if I addd the frameworks to both MyMainApp and MyLib but this is not what I want. I have made sure that the framework search paths and header search paths are correct. I've been unable to find any reference from Apple in such scenario. I'd like to know the best practice for adding/linking third party frameworks and libraries to an Xcode subproject of type static library. Also is there a solution to overcome the linker error and add frameworks to only the static library project?

我的 Xcode 项目“MyMainApp”有一个名为“MyLib”的静态库类型 Xcode 子项目。现在我需要在我的 iOS 应用程序中使用一些第三方 iOS 框架。我想仅针对 MyLib 而不是针对 MyMainApp 添加/链接它们,这样我就可以使 MyLib 可重用且独立,以便在我的其他 iOS 应用程序中使用它。MyMainApp 中的一些视图控制器使用作为 MyLib 一部分的视图控制器之一。MyLib 中的此视图控制器使用驻留在第三方框架内的类的功能。现在我面临的问题是,如果我将第三方框架仅添加到 MyLib 而不是将它们添加到 MyMainApp,我会收到链接器错误“词法或预处理器问题。找不到 XXX.h 文件”。如果我将框架添加到 MyMainApp 和 MyLib 中,一切正常,但这不是我想要的。我已经确保框架搜索路径和标题搜索路径是正确的。在这种情况下,我一直无法从 Apple 那里找到任何参考资料。我想知道将第三方框架和库添加/链接到静态库类型的 Xcode 子项目的最佳实践。还有一种解决方案可以克服链接器错误并将框架添加到静态库项目中吗?

回答by blockchaining

  1. Go to 'Build Phases' for 'MyLib'
  2. Add the '3rd party iOS Framework' under 'Copy Bundle Resouces'
  3. Compile 'MyLib'
  4. Remove '3rd party iOS Framework' from 'MyMainApp'
  5. Run 'MyMainApp'
  1. 转到“MyLib”的“构建阶段”
  2. 在“Copy Bundle Resouces”下添加“3rd party iOS Framework”
  3. 编译'MyLib'
  4. 从“MyMainApp”中删除“3rd party iOS Framework”
  5. 运行“MyMainApp”

回答by quellish

Your library will have to link against them, and applications using your library will have to link against them andinclude them in the application bundle. The application is responsible for satisfying the dependancy.

您的库必须链接到它们,使用您的库的应用程序必须链接到它们并将它们包含在应用程序包中。应用程序负责满足依赖关系。

Frameworks can contain resources (like the header files that are giving you issues), which is one of the primary things that differentiates them from libraries. Because of this, a library can't really contain a framework, even if it could contain the framework's binary objects (code).

框架可以包含资源(例如给您带来问题的头文件),这是将它们与库区分开来的主要内容之一。因此,库不能真正包含框架,即使它可以包含框架的二进制对象(代码)。

If your library is converted to a framework it CAN contain other frameworks within it, which may meet your needs.

如果您的库被转换为框架,它可以在其中包含其他框架,这可能会满足您的需求。

回答by Rivera

My Xcode project "MyMainApp" has a static library type Xcode subproject called "MyLib". Now I need to use some third party iOS frameworks in my iOS app. I want to add/link them against only MyLib and not against MyMainApp so I can make the MyLib reusable and self-contained to use it in my other iOS apps too.

我的 Xcode 项目“MyMainApp”有一个名为“MyLib”的静态库类型 Xcode 子项目。现在我需要在我的 iOS 应用程序中使用一些第三方 iOS 框架。我想仅针对 MyLib 而不是针对 MyMainApp 添加/链接它们,这样我就可以使 MyLib 可重用且独立,以便在我的其他 iOS 应用程序中使用它。

Libraries don't link themselves against other libraries, they just get build separately into unlinked *.abinaries. Later the application (and only then) will link all those *.afiles and its own binaries together.

库不会将自己与其他库链接,它们只是单独构建到未链接的*.a二进制文件中。稍后应用程序(并且只有那时)将所有这些*.a文件和它自己的二进制文件链接在一起。

You're right though that libraries should be kept modular and do not include other libraries code or resources to avoid version and compatibility problems.

虽然库应该保持模块化并且不包含其他库代码或资源以避免版本和兼容性问题,但您是对的。

Now the problem I am facing is if I add the third party frameworks to just MyLib and don't add them to MyMainApp, I get a linker error "Lexical or Preprocessor issue. XXX.h file not found". Everything works well if I addd the frameworks to both MyMainApp and MyLib but this is not what I want.

现在我面临的问题是,如果我将第三方框架仅添加到 MyLib 而不是将它们添加到 MyMainApp,我会收到链接器错误“词法或预处理器问题。找不到 XXX.h 文件”。如果我将框架添加到 MyMainApp 和 MyLib 中,一切正常,但这不是我想要的。

Your application is indirectly trying to import one of the 3rd party headers. While you don't need to reference the 3rd party library code you still need to have access to its header files to compile. In other words you don't need to add the 3rd party library to your application but you do have to add the header files or set the header search path.

您的应用程序正在间接尝试导入第 3 方标头之一。虽然您不需要引用第 3 方库代码,但您仍然需要访问其头文件进行编译。换句话说,您不需要将第 3 方库添加到您的应用程序中,但您必须添加头文件或设置头搜索路径。

I'd like to know the best practice for adding/linking third party frameworks and libraries to an Xcode subproject of type static library. Also is there a solution to overcome the linker error and add frameworks to only the static library project?

我想知道将第三方框架和库添加/链接到静态库类型的 Xcode 子项目的最佳实践。还有一种解决方案可以克服链接器错误并将框架添加到静态库项目中吗?

There's no easy "native" way to handle libraries, their resources and dependencies on iOS, so you should take a look at CocoaPods.

没有简单的“本地”方式来处理库、它们的资源和 iOS 上的依赖项,所以你应该看看CocoaPods