您可以为 iOS 构建动态库并在运行时加载它们吗?

发布于 2024-10-12 10:49:00 字数 429 浏览 8 评论 0 原文

iOS (iPhone/iPad) 是否支持动态库?

在 Xcode 中,我尝试创建一个新项目 -> 框架与图书馆 -> Cocoa 库(动态)。在项目设置中,我将 Base SDK 设置为 iOS 设备 4.1 并将目标设置为 iOS4.1,但出现构建错误:

目标指定产品类型“com.apple.product-type.library.dynamic”,但“iphonesimulator”平台没有此类产品类型。

我选择的构建是模拟器 -> 调试 -> i386< /强>。

Are dynamic libraries supported on iOS (iPhone/iPad)?

In Xcode, I tried to create a New project -> Framework & Library -> Cocoa Library (dynamic). In the project settings, I set the Base SDK to iOS device 4.1 and target to iOS4.1, but it has a build error:

target specifies product type 'com.apple.product-type.library.dynamic', but there's no such product type for the 'iphonesimulator' platform".

The build i selected is Simulator -> Debug -> i386.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

够运 2024-10-19 10:49:00

我并不是真的不同意DarkDust的回答,但如果我可以传达我内心的比尔·克林顿的话,这取决于支持的含义 :)

Apple 不希望您对 App Store 应用程序执行此操作,但操作系统当然允许这样做。越狱应用程序一直使用这种技术。您基本上使用标准 UNIX 技术来动态打开框架/库,然后使用其中的内容。 dlopen 函数 允许您可以通过传入该框架的路径或dylib来打开库。来自一些构建越狱应用程序的文档,这是一个调用在您自己的单独 dylib 中实现的 init() 函数的示例:

#include <dlfcn.h>

initWrapper() {
    char *dylibPath = "/Applications/myapp.app/mydylib2.dylib";
    
    void *libHandle = dlopen(dylibPath, RTLD_NOW);
    if (libHandle != NULL) {
        // This assumes your dylib’s init function is called init, 
        //    if not change the name in "".
        void (*init)() = dlsym(libHandle, "init");
        if (init != NULL)  {
            init();
        }
        dlclose(libHandle);
    }
}

此外,默认限制不允许您构建 iOS 动态库项目是 Xcode 中的某些内容,您可以通过编辑一些 XCode xml 文件来覆盖:

Build and use dylib on iOS

一旦完成此操作,您就可以构建一个普通的 iOS .dylib 库,并根据上面的示例代码使用它。 (是的,每当您安装新的 XCode 版本时,您可能都必须再次解锁此功能)。

因此,这不是技术限制,而是 App Store 政策限制。如果您不限于 App Store,那么您可以做到。请注意,此技术不需要需要越狱,但如果应用程序是沙盒的,它可能会限制 dylib 的加载位置。

编辑:为了确保此信息不会因将来的链接失效而丢失,以下是我提供的有关如何在 Xcode 中启用 iOS dylibs 的链接内容。 (注意:此过程在 Xcode 4 上仍然有效,但请参阅下面的注释以了解路径更新等。)来源是 iOS Place 博客


Xcode 不允许您为 iOS 构建 dylib。如果不是单一二进制文件,应用程序将被拒绝。但我有一个具有插件架构的应用程序来加载可选模块。我只是想要一个快速原型来证明概念,然后再将其完全移植到 iOS。如果 dylib 可以简单地工作,那么速度会更快。因此,这篇文章展示了如何构建和使用 dylib,但请注意它不会被 App Store 批准。 (在 10.6.4 上使用 Xcode 3.2.4 进行测试)

1. 在属性列表编辑器中打开这些文件:/Developer/Platforms/MacOSX.platform/Developer/Library/Xcode/Specifications/ MacOSX Product Types.xcspec/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Specifications/iPhone Simulator ProductTypes.xcspec

2. 找到“MacOSX Product Types.xcspec”中具有产品类型 com.apple.product-type.library.dynamic 的项目,并将其拖动到“iPhone模拟器 ProductTypes.xcspec”。

Xcode 截图 1

3. 打开“MacOSX Package Types.xcspec ”和“iPhone Simulator PackageTypes.xcspec”在同一位置找到。

4. 在“MacOSX Product Types.xcspec”中找到包类型为 com.apple.package-type.mach-o-dylib< 的项目/code> 并将其拖至“iPhone Simulator PackageTypes.xcspec”。

Xcode Screenshot 2

5. 对“iPhoneOS.platform” 并重新启动 Xcode(如果它正在运行)。

现在,让我们构建一个 dylib。从“Cocoa Touch Static Library”模板开始。这应该包括项目中的 Foundation.framework。以下是我在构建 dylib 的模板之上所做的更改。

1. 在文本编辑器中打开文件 project.pbxproj(可在 Xcode 项目文件包中找到)。搜索字符串“producttype”,将其值更改为com.apple.product-type.library.dynamic

现在,使用Xcode打开项目,进入项目->编辑项目设置

2.安装目录”设置为@ executable_path/ 因为我计划将 dylib 放在与应用程序的可执行文件相同的目录中。

3.Mach-O Type”设置为动态库

4.可执行扩展”设置为 dylib

< strong>5.“可执行前缀”设置为空

6.向库添加一两个简单方法并构建它。

现在,创建一个应用程序来测试它。这次,我选择基于视图的应用程序。连接 UIButton 和 UILabel 来调用库并显示返回消息。您可以下载完整的项目TestApp并使用它。

失落的知识:
下面的屏幕截图与现在不起作用的链接相关。
在 iOS 上构建和使用 dylib
输入图片此处描述
输入图片此处描述

I'm not really disagreeing with DarkDust's answer, but if I may channel my inner Bill Clinton, it depends on what the meaning of supported is :)

Apple doesn't want you doing this for App Store apps, but the operating system certainly allows it. Jailbreak apps use this technique all the time. You basically use a standard UNIX technique to dynamically open a framework/library, and then use stuff in it. The dlopen function allows you to open the library by passing in the path to that framework, or dylib. From some docs for building jailbreak apps, here's an example of calling an init() function implemented inside your own, separate dylib:

#include <dlfcn.h>

initWrapper() {
    char *dylibPath = "/Applications/myapp.app/mydylib2.dylib";
    
    void *libHandle = dlopen(dylibPath, RTLD_NOW);
    if (libHandle != NULL) {
        // This assumes your dylib’s init function is called init, 
        //    if not change the name in "".
        void (*init)() = dlsym(libHandle, "init");
        if (init != NULL)  {
            init();
        }
        dlclose(libHandle);
    }
}

Furthermore, the default restriction against allowing you to build a dynamic library project for iOS is something in Xcode that you have the ability to override by editing some XCode xml files:

Build and use dylib on iOS

Once you do this, you can build a normal iOS .dylib library, and use it per the sample code above. (yes, you probably will have to unlock this capability again whenever you install a new XCode version).

So, it's not a technical limitation, but an App Store policy limitation. If you're not limited to the App Store, then you can do it. Note that this technique does not require jailbreaking, although if the app is sandboxed, it may limit where dylibs can be loaded from.

Edit: in order to make sure this information isn't lost to future link rot, here is the content of the link I provided on how to enable iOS dylibs in Xcode. (Note: this process still works on Xcode 4, but see comment(s) below for updates to paths, etc.) Source is the iOS Place blog:


Xcode does not allow you to build dylib for iOS. App will be rejected if it’s not single binary. But I have an application that has plug-in architecture to load optional modules. I just want a quick prototype to prove concept before fully port it to iOS. It’s faster to do if dylib could simply work. So, this post shows how to build and use dylib but be aware it won’t be approved to App Store. (Tested with Xcode 3.2.4 on 10.6.4)

1. Open these files in the Property List Editor: /Developer/Platforms/MacOSX.platform/Developer/Library/Xcode/Specifications/MacOSX Product Types.xcspec and /Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Specifications/iPhone Simulator ProductTypes.xcspec

2. Locate the item in the “MacOSX Product Types.xcspec” that has the product type com.apple.product-type.library.dynamic and drag it to the “iPhone Simulator ProductTypes.xcspec”.

Xcode screenshot 1

3. Open “MacOSX Package Types.xcspec” and “iPhone Simulator PackageTypes.xcspec” found at the same places.

4. Locate the item in the “MacOSX Product Types.xcspec” that has the package type com.apple.package-type.mach-o-dylib and drag it to the “iPhone Simulator PackageTypes.xcspec”.

Xcode screenshot 2

5. Repeat the steps for the “iPhoneOS.platform” and relaunch Xcode if it was running.

Now, lets build a dylib. Start out with the “Cocoa Touch Static Library” Template. That should included the Foundation.framework in the project. Here are the changes I made on top of the template to build dylib.

1. Open the file project.pbxproj (found inside the Xcode project file bundle) in a Text Editor. Search for string “producttype”, change it’s value to com.apple.product-type.library.dynamic;

Now, open the project with Xcode, go to Project->Edit Project Settings

2.Installation Directory” set to @executable_path/ because I plan to put the dylib in the same directory as the app’s executable.

3.Mach-O Type” set to Dynamic Library

4.Executable Extension” set to dylib

5.Executable Prefix” set to empty

6. Add one or two simple methods to the library and build it.

Now, create an app to test it. This time, I choose the View-based Application. Hook up a UIButton and a UILabel to call the lib and showing return message. You can download the complete project TestApp and play with it.

The Lost knowledge:
Screenshots below are related to links not working now.
Build and use dylib on iOS
enter image description here
enter image description here

挽袖吟 2024-10-19 10:49:00

在提出这个问题时,iOS 不支持动态库,这将导致您的应用程序被拒绝。仅允许静态库。

然而,在iOS8中你可以使用动态库和框架。它应该“正常工作”

At the time this question was asked, Dynamic libraries were not supported by iOS and will result in your app getting rejected. Only static libraries are allowed.

However, in iOS8 you can use dynamic libraries and frameworks. It should "just work"

故笙诉离歌 2024-10-19 10:49:00

从 Xcode 11.4.1 开始,不允许使用动态库(您的项目不会针对所有目标进行编译)。使用 libs/frameworks 的新方法是来自 xcodebuild 的 create-xcframework。

Starting from Xcode 11.4.1 dynamic libraries are not allowed (your project won't compile for all destinations). The new way to use libs/frameworks is the create-xcframework from xcodebuild.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文