Is the function 'dlopen()' private API?

别说谁变了你拦得住时间么 提交于 2019-12-28 12:01:22

问题


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


回答1:


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!

[EDIT] - 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

[EDIT] - contrived example

#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]);
    }
}


来源:https://stackoverflow.com/questions/6530701/is-the-function-dlopen-private-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!