xcode 如何在 iPhone 项目中添加第三方框架?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/505974/
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-14 18:28:29  来源:igfitidea点击:

How do I add a third party Framework to iPhone project?

iphonecocoaxcodedyld

提问by talanb

I am trying to use the PLDatabase framework in my iPhone app. I've added the framework to my Xcode project. However, when I run my app, it crashes with the following error:

我正在尝试在我的 iPhone 应用程序中使用 PLDatabase 框架。我已将框架添加到我的 Xcode 项目中。但是,当我运行我的应用程序时,它崩溃并显示以下错误:

dyld: Library not loaded: @loader_path/../Frameworks/PlausibleDatabase.framework/Versions/A/PlausibleDatabase
  Referenced from: /Users/todd/Library/Application Support/iPhone Simulator/User/Applications/BB3C66B2-A5BB-4329-B163-AB0072411AF1/Congress.app/Congress
  Reason: image not found

I'm not sure exactly where the Framework needs to reside on disk to be found.

我不确定框架需要驻留在磁盘上的确切位置才能找到。

Thanks

谢谢

回答by Louis Gerbarg

iPhone does not support dynamic linking of embedded frameworks. While you might be able to finagle the simulator to work, it will not work on the device. If you want to use the code you must build the static library for the framework (libpldatabase.a), and then link them to the app.

iPhone 不支持嵌入式框架的动态链接。虽然您可能能够使模拟器工作,但它在设备上不起作用。如果要使用代码,则必须为框架构建静态库 (libpldatabase.a),然后将它们链接到应用程序。

The Xcode GUI does not provide a nice interface for static linking, you will need to add appropriate flags in your build prefs (-lpldatabase -L/whatever/dir/it/is/in).

Xcode GUI 没有为静态链接提供一个很好的界面,您需要在构建首选项中添加适当的标志 (-lpldatabase -L/whatever/dir/it/is/in)。

回答by Aftermathew

It's not actually strictly true that the iPhone doesn't support dynamic linking. What is true is that applications installed by the App Store are unable to dynamically link.

iPhone 不支持动态链接实际上并不完全正确。确实,App Store 安装的应用程序无法动态链接。

The app store / ituned installs programs into the /private/var/mobile/... directory. Any program opened that lives in that subdirectory is chrooted and has certain rights stripped away when opened. The chrooted processes can't fork, they can't run in the background, they can't load dynamic libraries and they can't save files outside of their little protected areas of the disk (with the exception of photos to the photo directory).

app store / ituned 将程序安装到 /private/var/mobile/... 目录中。位于该子目录中的任何打开的程序都会被 chroot 并在打开时被剥夺某些权限。chrooted 进程不能 fork,不能在后台运行,不能加载动态库,也不能将文件保存在磁盘的小保护区域之外(除了照片到照片目录)。

That said, the iPhone runs a modern operating system that supports dynamic linking just fine. The act of 'jailbreaking' is actually installing a program outside the chroot jail that can then do things like fork and save files to other places on the disk and load dynamic code.

也就是说,iPhone 运行的现代操作系统支持动态链接就好了。“越狱”的行为实际上是在 chroot jail 之外安装一个程序,然后该程序可以执行诸如 fork 之类的操作,并将文件保存到磁盘上的其他位置并加载动态代码。

Apple (and the open iPhone community) has plenty of programs running on the phone (such as the MobilePhone, Mobile Safari and SpringBoard applications) that can run in the background and load libraries. They are placed in a different place on the disk (/private/var/stash/Applications often).

苹果(以及开放的 iPhone 社区)有很多在手机上运行的程序(例如 MobilePhone、Mobile Safari 和 SpringBoard 应用程序),可以在后台运行并加载库。它们被放置在磁盘上的不同位置(通常是 /private/var/stash/Applications)。

So... if you want to sell your app in the app store, you can't load a dynamic library. Which for most people means you can't load it at all. But if you want to distribute your app through cydia (a common jailbroken phone app installer), then you can get away with jailbreaking the phone and loading your dynaamic library. In fact, due to the itunes install process being the culprit, as you've learned, you cant even load a dylib from your own app that you write to you own phone... unless you jailbreak.

所以...如果你想在应用商店销售你的应用,你不能加载动态库。对于大多数人来说,这意味着您根本无法加载它。但是,如果您想通过 cydia(一种常见的越狱手机应用安装程序)分发您的应用,那么您可以通过越狱手机并加载动态库来摆脱困境。事实上,由于 iTunes 安装过程是罪魁祸首,正如您所了解的,您甚至无法从自己的应用程序中加载 dylib,而这些应用程序是您写入自己手机的……除非您越狱。

回答by cdespinosa

RE @mipadi: ZeroLink was removed in Xcode 3.1 and does not exist for the iPhone SDK. The correct answer is Louis'; the iPhone does not support dynamically-loaded frameworks in developer-created applications.

RE @mipadi:ZeroLink 已在 Xcode 3.1 中删除,iPhone SDK 中不存在。正确答案是路易斯的;iPhone 不支持开发人员创建的应用程序中的动态加载框架。

Xcode does have a good user interface for static libraries; just drag them into the project and they're added to the link phase. No need to fuss with linker flags. The problem comes when you need to use the headers supplied with those static libs (then you need to add the header search paths manually) or when a static lib conflicts with an available dylib (that's when you have to add the -l flag manually).

Xcode 确实为静态库提供了良好的用户界面;只需将它们拖到项目中,它们就会被添加到链接阶段。无需为链接器标志大惊小怪。当您需要使用这些静态库提供的标头(然后您需要手动添加标头搜索路径)或静态库与可用的 dylib 冲突时(此时您必须手动添加 -l 标志),就会出现问题.