我可以在我的主机应用程序中读取我的应用程序扩展的 info.plist 吗?

Can I read my app extension's info.plist in my host app?

我构建了一个今日小部件,我想在我的主机应用程序的“关于”屏幕中显示 运行 的版本。有没有办法做到这一点?

我尝试用 bundleWithIdentifier 替换 [NSBundle mainBundle] 但失败了。那么,有没有办法从他的扩展包中获取 infoDictionary,或者我是在浪费时间吗?

你不能。应用程序和扩展程序无法访问彼此的代码和地址 space。他们只能使用使用 AppGroup 共享容器或使用 Shared NSUserDefault 来共享数据。

使用这两种方法都需要 运行 您的扩展程序至少一次。

您可以通过指定扩展包的直接路径来完成此操作。应用扩展似乎位于应用主包的 PlugIns/ 文件夹中。您可以使用 -initWithPath: 创建 NSBundle 的实例。例如:

- (NSBundle *)appExtensionBundle {
    NSString *plugInsPath = [NSBundle mainBundle].builtInPlugInsPath;
    NSString *appExtensionPath = [plugInsPath stringByAppendingPathComponent:@"MyTodayExtension.appex"];
    return [[NSBundle alloc] initWithPath:appExtensionPath];
}

注意:此代码仅使用 Today 扩展进行了测试。如果您有其他类型的扩展,您应该测试以确保它能正常工作。