iOS:objc_copyClassNamesForImage无法在设备上运行

时间:2016-11-04 01:11:45

标签: ios runtime

我有一个测试应用程序(它没有做什么因为我用它来测试一个bug),其中包括我构建的3个框架。使用复制文件阶段将框架复制到应用程序的Frameworks目录中。我有以下app委托代码:

#import "AppDelegate.h"
@import ObjectiveC;

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [[NSBundle allFrameworks] enumerateObjectsUsingBlock:^(NSBundle *framework, NSUInteger idx, BOOL *stop) {
        unsigned int count = 0;
        __unused const char** classes = objc_copyClassNamesForImage([[framework executablePath] UTF8String], &count);
        NSRange inFramework = [framework.executablePath rangeOfString:@".app/Frameworks"];
        if (inFramework.length > 0) {
            NSLog(@"Framework %@, classes: %i", framework.executablePath.lastPathComponent, count);
        }
    }];
    return YES;
}

@end

其中打印出Frameworks目录中的框架以及每个框架中包含的类的数量。

当我在模拟器中运行此代码时,我得到以下结果:

2016-11-04 12:02:17.682 RuntimeTest[54326:623229] Framework PEGKit, classes: 24
2016-11-04 12:02:17.705 RuntimeTest[54326:623229] Framework Alchemic, classes: 57
2016-11-04 12:02:17.707 RuntimeTest[54326:623229] Framework StoryTeller, classes: 10

然而,当我在设备上运行它时,我得到了:

2016-11-04 12:07:04.215417 RuntimeTest[1035:365233] Framework PEGKit, classes: 0
2016-11-04 12:07:04.224495 RuntimeTest[1035:365233] Framework Alchemic, classes: 0
2016-11-04 12:07:04.254946 RuntimeTest[1035:365233] Framework StoryTeller, classes: 0

该设备是配备iOS 10.1的iPhone 7。我很确定这段代码在过去是有用的,看来objc_copyClassNamesForImage在某种程度上被打破了。

我的工作理论是这可能是一些10.1错误。或者可能是在构建属性中设置/未设置的内容。

有谁能证实这一点?或者知道可能出现什么问题?

2 个答案:

答案 0 :(得分:0)

在设备上,您应该使用/private将路径前缀添加到框架图像。如果您询问包的路径,则会看到它具有/private前缀。这就是我找到解决方法的方法。

答案 1 :(得分:0)

您的代码对我来说很完美。enter image description here

相关问题