此方法将从任何目标获取您的捆绑包。但是,对于您添加的每个目标,您必须手动将 plist 捆绑标识符添加到identifiers
数组中,因为无法以编程方式获取它。优点是您可以使用相同的代码来测试或运行应用程序。
+(NSBundle*) getBundle
{
NSBundle *bundle = nil;
// try your manually set bundles
NSArray *identifiers = [NSArray arrayWithObjects: @"com.your.application",
@"com.your.test",
nil];
for(NSString *bundleId in identifiers) {
bundle = [NSBundle bundleWithIdentifier:bundleId];
if (bundle!=nil) break;
}
// try the main bundle
if (bundle==nil) bundle = [NSBundle mainBundle];
// abort
assert(bundle!=nil && "Missing bundle. Check the Bundle identifier on
the plist of this target vs the identifiers array in this class.");
return bundle;
}