[英]How can I include my resource files in my framework without using a separate bundle?
I've been following this guide to create an iOS static library: https://github.com/jverkoey/iOS-Framework#walkthrough . 我一直在按照本指南创建一个iOS静态库: https : //github.com/jverkoey/iOS-Framework#walkthrough 。 I managed to create a framework that can be imported into another Xcode project.
我设法创建了一个可以导入另一个Xcode项目的框架。
Now, I want my framework to be able to show a Storyboard. 现在,我希望我的框架能够显示故事板。 I imagine that the
.storyboard
file counts as a resource. 我想
.storyboard
文件算作资源。 The previous guide states that I should create a Bundle for my resource files, such as images, instead of putting them in the framework itself. 前面的指南指出我应该为我的资源文件创建一个Bundle,比如图像,而不是将它们放在框架本身。
However, I do want to put them in the framework itself. 但是,我确实希望将它们放在框架中。 I don't want to use a separate bundle.
我不想使用单独的捆绑包。
All guides I find around tell me the same thing. 我找到的所有指南都告诉我同样的事情。 I understand the advantages of using a separate bundle, but I just don't want to do it right now.
我理解使用单独的捆绑包的优点,但我现在不想这样做。
Now then, I am lost on how to include my .storyboard
file in my framework so that I can show it with this: 现在,我迷失了如何在我的框架中包含我的
.storyboard
文件,以便我可以用它来显示它:
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"NameOfStoryboard" bundle:[NSBundle bundleForClass:[self class]]];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"NameOfViewController"];
[otherView presentViewController:vc animated:YES completion:NULL];
The above code does not seem to work (it can't find the storyboard file). 上面的代码似乎不起作用(它找不到storyboard文件)。 I imagine this is because the
.storyboard
is not currently included in the framework. 我想这是因为
.storyboard
目前不包含在框架中。 Sadly, I don't know how to include it. 可悲的是,我不知道如何包含它。
In the framework's Xcode project, under Build Phases, I see a tab labeled "Copy files" which looks like what I need. 在框架的Xcode项目中,在Build Phases下,我看到一个标签为“Copy files”的标签,看起来就像我需要的那样。
Now, I'm not sure what this is doing, but in any case, it doesn't seem to be working anyway. 现在,我不确定这是做什么的,但无论如何,它似乎并没有起作用。
I also did this: 我也这样做了:
How can I include my .storyboard
file in my framework without using a separate bundle? 如何在不使用单独的捆绑包的情况下在我的框架中包含我的
.storyboard
文件?
Why don't you want to use separate bundle for a framework? 为什么不想为框架使用单独的bundle? So if an app is using your framework it would be able to use resources from it(Xibs, storyboards, whatever)
因此,如果一个应用程序正在使用您的框架,它将能够使用它的资源(Xibs,故事板,等等)
For example a call from the app that uses your framework can look something like this: 例如,来自使用您的框架的应用程序的调用可能如下所示:
FrameworkViewController *frameworkView = [[FrameworkViewController alloc] initWithNibName:@"FrameworkViewController"
bundle:[NSBundle yourFrameworkBundle]];
and in your framework you can write a helper class NSBundle+YourFrameworkBundle that gives access to framework bundle: 在您的框架中,您可以编写一个帮助类NSBundle + YourFrameworkBundle,它可以访问框架包:
NSBundle+YourFrameworkBundle.h 一个NSBundle + YourFrameworkBundle.h
@interface NSBundle (YourFrameworkBundle)
+ (NSBundle *)yourFrameworkBundle;
@end
NSBundle+YourFrameworkBundle.m 一个NSBundle + YourFrameworkBundle.m
#import "NSBundle+YourFrameworkBundle.h"
@implementation NSBundle (YourFrameworkBundle)
+ (NSBundle *)yourFrameworkBundle
{
static NSBundle *frameworkBundle = nil;
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
NSString *mainBundlePath = [[NSBundle mainBundle] resourcePath];
frameworkBundle = [NSBundle bundleWithPath:[mainBundlePath stringByAppendingPathComponent:@"YourFrameworkBundle.bundle"]];
});
return frameworkBundle;
}
@end
To bundle Resources for the framework create a separate target where you would link all your resources. 要为框架捆绑资源,请创建一个单独的目标,您可以在其中链接所有资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.