23

関数「dlopen()」を使用してiOSプラットフォームでダイナミックライブラリを呼び出したいのですが、関数「dlopen()」はプライベートAPIですか?

4

1 に答える 1

25

私はiOSでdlopenを何年も使用して成功しています。私のユースケースでは、アプリの起動時にロードするのではなく、dlopenを使用してパブリックシステムフレームワークをオンデマンドでロードします。よく働く!

[編集]-iOS8以降、拡張機能と共有フレームワークの使用は禁止されてdlopenいますが、アプリケーション自体は引き続き使用できますdlopen(Appleフレームワークだけでなく、カスタムフレームワークでもサポートされていることが文書化されています)。このAppleドキュメントの「古いバージョンのiOSへの含まれるアプリのデプロイ」セクションを参照してください: 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]);
    }
}
于 2014-06-17T14:28:39.193 に答える