函数'dlopen()'是私有API吗? [英] Is the function 'dlopen()' private API?

查看:205
本文介绍了函数'dlopen()'是私有API吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用函数'dlopen()'在iOS平台上调用动态库,是'dlopen()'私有API函数吗?

I want use function 'dlopen()' to invoke a dynamic library on iOS platform, is the function 'dlopen()' private API?

推荐答案

多年来我在iOS上使用dlopen取得了成功。在我的用例中,我使用dlopen按需加载公共系统框架,而不是在应用程序启动时加载它们。效果很好!

I've had success using dlopen on iOS for years. In my use case, I use dlopen to load public system frameworks on demand instead of having them loaded on app launch. Works great!

- 从iOS 8开始,扩展和共享框架禁止使用 dlopen ,但是应用程序本身仍然可以使用 dlopen (现在记录为不仅支持Apple框架,还支持自定义框架)。请参阅此Apple doc中的将包含应用程序部署到iOS的旧版本部分:https://developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensibilityPG.pdf

- as of iOS 8, extensions and shared frameworks are prohibited from using dlopen, however the application itself can still use dlopen (and is now documented as being supported for not only Apple frameworks, but custom frameworks too). See the Deploying a Containing App to Older Versions of iOS section in this Apple doc: https://developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensibilityPG.pdf

- 人为的例子

#import <dlfcn.h>

void printApplicationState()
{
    Class UIApplicationClass = NSClassFromString(@"UIApplication");
    if (Nil == UIApplicationClass) {
        void *handle = dlopen("System/Library/Frameworks/UIKit.framework/UIKit", RTLD_NOW);
        if (handle) {
            UIApplicationClass = NSClassFromString(@"UIApplication");
            assert(UIApplicationClass != Nil);
            NSInteger applicationState = [UIApplicationClass applicationState];
            printf("app state: %ti\n", applicationState);
            if (0 != dlclose(handle)) {
                printf("dlclose failed! %s\n", dlerror());
            }
        } else {
            printf("dlopen failed! %s\n", dlerror());
        }
    } else {
        printf("app state: %ti\n", [UIApplicationClass applicationState]);
    }
}

这篇关于函数'dlopen()'是私有API吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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