建立一个使用cocoapods的可分发静态库 [英] Building a distributable static library that uses cocoapods

查看:69
本文介绍了建立一个使用cocoapods的可分发静态库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建要分发给其他iOS开发人员的静态库,并且在配置链接器以允许将静态库用于其他应用程序时遇到了一些麻烦.我已使用本指南来创建一个MyStaticLibrary.framework捆绑包,其中包含lib本身以及其他资源(例如图像).这样可以成功构建并使用cocoapods来获取所需的依赖项(AFNetworking等).到目前为止,一切都很好.

I'm building a static library to be distributed to other iOS developers and having some trouble configuring the linker to allow the static library to be used in another app. I have used this guide to create a MyStaticLibrary.framework bundle containing the lib itself, and other assets such as images. This builds successfully and uses cocoapods to source the required dependencies (AFNetworking, etc.). So far, so good.

但是,当我将MyStaticLibrary.framework导入新的Xcode项目以测试使用该库构建应用程序时,我收到大量的链接器错误(体系结构i386的未定义符号,_OBJC_CLASS _ $ _ CLASSNAME),这表明我做错了什么在这里.

But when I import MyStaticLibrary.framework into a new Xcode project to test build an app with the library, I get tons of linker errors (Undefined symbols for architecture i386, _OBJC_CLASS_$_CLASSNAME) indicating that I'm doing something very wrong here.

所以我的问题是,如何使用来自cocoapods的依赖项来构建MyStaticLibrary.framework,这样我就可以仅向第三方提供我的框架文件,并允许他们访问公共标头中指定的所有功能?

So my question is, how can I build MyStaticLibrary.framework with the dependencies sourced from cocoapods, such that I can provide a 3rd party with just my framework file and allow them access to all functions specified in the public headers?

推荐答案

默认情况下,您使用CocoaPods包含的任何库将编译到您的框架中-它们是作为外部依赖项而存在的不属于您的实际产品.但是,根据他们的常见问题解答,他们支持一种模式,您可以下载pod并将其链接到您的项目.从他们的常见问题解答中:

Any libraries you include using CocoaPods will not be compiled into your framework by default - they're meant to be external dependencies that are not part of your actual product. However, according to their FAQ, they support a mode where you can download pods and not have them linked to your project. From their FAQ:

请注意,CocoaPods本身不需要使用工作区.如果 您更喜欢使用子项目,可以通过运行pod install进行操作 --no-integrate,这将使集成到您的项目中完全由您决定.

Note that CocoaPods itself does not require the use of a workspace. If you prefer to use sub-projects, you can do so by running pod install --no-integrate, which will leave integration into your project up to you as you see fit.

要在已编译的二进制文件中包含外部依赖项:

To include external dependencies in your compiled binary:

  • 对于代码:不用使用cocoapods,而是签出要包含的存储库,然后将源文件复制到项目中-这将确保它们与其余代码一起编译

  • For code: Instead of using cocoapods, check out the repositories you want to include and copy the source files into your project -- this will ensure they are compiled with the rest of your code

对于静态库(即.a文件),请在框架的Link Binary With Libraries构建阶段中,确保包括所有要编译的库.您还应该确保相关的头文件包含在Copy Headers构建阶段中,并且具有适当的可见性.

For static libraries (i.e. .a files), in your framework's Link Binary With Libraries build phase, make sure to include all the ones you would like to compile. You should also make sure the associated header files are included in Copy Headers build phase, with the appropriate visibility.

注意以这种方式捆绑第三方库时,您将冒与集成框架的项目冲突的风险.例如,假设您正在使用一个名为SOSomeView的库,并且选择将其与您的框架一起编译.现在,如果要与之集成的应用程序还包含SOSomeView,您将收到一个编译时错误,该类被声明了两次.要解决此问题,您应该重新命名要硬编码到框架中的所有外部依赖项(即,将该类重命名为XXSOSomeView).

Note When bundling third party libraries in this way, you run the risk of conflicting with the projects that are integrating your framework. For example, let's say you are using a lib called SOSomeView, and you choose to compile that in with your framework. Now, if the app you are integrating with also includes SOSomeView, you will get a compile-time error that the class is declared twice. To fix this issue, you should re-namespace any external dependencies you want to hardcode into your framework (i.e. rename the class to XXSOSomeView).

如果您在框架中编译静态库,我不知道如何解决该问题.

I don't know how to solve that problem if you are compiling static libraries in with your framework.

这篇关于建立一个使用cocoapods的可分发静态库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆