我不再手动管理框架,而是使用推荐的CocoaPods。
原始答案:
- Use the fake framework @wattson12 mentioned. It will compile and save the resources as well.
Inspired by this script, add this to your target to copy the resources to your app:
SOURCE_PATH="${TARGET_BUILD_DIR}/MYFramework.framework/Resources/"
TARGET_PATH="${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/MYFrameworkResources.bundle"
mkdir -p $TARGET_PATH
cp -R $SOURCE_PATH $TARGET_PATH
你也可以将框架拖到“复制资源”步骤中,但这样会添加不必要的头文件和编译代码。
编辑:
例如,要在IB中使用这些资源(如png文件),请执行以下操作:
MyImage
由:
MYFrameworkResources.bundle/MyImage.png
它会显示一个损坏的图片图标,但在运行时会正常工作。
从代码中加载Nib:
[NSBundle loadNibNamed:@"MYFrameworkResources.bundle/MyNib" ...
最后,您可以将这些方法添加到NSBundle类别中,以便轻松访问可能在主捆绑包或MYFrameworkResources.bundle中的Nib资源。
@implementation NSBundle (MyCategory)
+ (NSString *)pathForResource:(NSString *)name
ofType:(NSString *)extension
{
NSBundle * mainBundle = [NSBundle mainBundle];
NSString * path = [mainBundle pathForResource:name
ofType:extension];
if (path)
{
return path;
}
NSBundle * bundle;
for (NSString * bundlePath in [mainBundle pathsForResourcesOfType:@"bundle"
inDirectory:nil])
{
bundle = [NSBundle bundleWithPath:bundlePath];
path = [bundle pathForResource:name
ofType:extension];
if (path)
{
return path;
}
}
NSLog(@"No path found for: %@ (.%@)", name, extension);
return nil;
}
+ (NSArray *)loadNibNamed:(NSString *)name
owner:(id)owner
options:(NSDictionary *)options
{
NSBundle * mainBundle = [NSBundle mainBundle];
if ([mainBundle pathForResource:name
ofType:@"nib"])
{
NSLog(@"Loaded Nib named: '%@' from mainBundle", name);
return [mainBundle loadNibNamed:name
owner:owner
options:options];
}
NSBundle * bundle;
for (NSString * bundlePath in [mainBundle pathsForResourcesOfType:@"bundle"
inDirectory:nil])
{
bundle = [NSBundle bundleWithPath:bundlePath];
if ([bundle pathForResource:name
ofType:@"nib"])
{
NSLog(@"Loaded Nib named: '%@' from bundle: '%@' ", name, bundle.bundleIdentifier);
return [bundle loadNibNamed:name
owner:owner
options:options];
}
}
NSLog(@"Couldn't load Nib named: %@", name);
return nil;
}
@end
它将首先查找您的应用程序包,然后查找MYFrameworkResources.bundle等。