如何从主程序检索 iOS 框架包中的文件/资源​​? [英] How to retrieve a file/resource in a iOS framework bundle from a main program?

查看:26
本文介绍了如何从主程序检索 iOS 框架包中的文件/资源​​?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用有关为 iOS 开发创建通用二进制框架的方法来分发库.我还想在框架中添加文件,并将它们加载到主程序中(实际上是 SSL 证书).这与这个 StackOverflow 问题,主要区别在于我想包含除 XIB 之外的其他内容.

I use methods about creating a universal binary framework for iOS development to distribute a library. I would also like to add files in the framework, and load them in the main program (an SSL certificate actually). This is related to this StackOverflow question, the main difference is that I want to include something else than a XIB.

如果我们阅读与链接帖子相关的线程,似乎不可能.但是,提到了以下博客文章;我们可以阅读:

If we read the thread related to the linked post, it seems it is not possible. However, the following blog article was mentioned; we can read:

"静态框架不能完全替代标准框架-- 访问框架资源的标准方法,例如 +[NSBundle bundleForClass:] 不会返回预期的包.包路径通过查询库路径的dyld来确定对应符号地址——在静态链接的情况下,这将返回静态库所在的二进制文件的路径被链接.此外,NSBundle/CFBundle 将检查之前的堆栈 from 作为返回地址,并使用该返回地址确定调用代码的 dyld 二进制路径.鉴于这些限制,一些变通办法是可能的,例如使用 -[NSBundle privateFrameworkPath]定位到嵌入式框架的路径."

"Static frameworks are not a full replacement for standard frameworks -- standard methods for accessing the framework resources, such as +[NSBundle bundleForClass:] will not return the expected bundle. The bundle path is determined by querying dyld for the library path corresponding to the symbol address -- In the case of static linking, this will return the path to the binary into which the static library was linked. Additionally, NSBundle/CFBundle will examine the previous stack from for the return address, and use that return address to determine the calling code's dyld binary path. Given these restrictions, some work-arounds are possible, such as using -[NSBundle privateFrameworkPath] to locate the path to the embedded framework."

我是 iOS 开发的初学者,我很难使用这种机制.使用 privateFrameworksPath 确实返回了一个路径,但我不知道接下来要做什么才能找到我想要加载的文件.

I am a beginner in iOS development, and I struggle in using such a mechanism. Using the privateFrameworksPath returns a path indeed, but I do not know what to do next to locate the file I want to load.

就是这样:

NSBundle * bundle = [NSBundle bundleForClass:[self class]];
NSString * path = [bundle privateFrameworksPath]; // Works, return something like .../Frameworks
NSString * file = [path stringByAppendingPathComponent:@"MyFramework.framework/Versions/A/my-cert.der"]; 
NSData * trustedCertData = [NSData dataWithContentsOfFile:file]; // Returns nil

有什么想法吗?

推荐答案

以下代码在类似情况下对我有用

The following code worked for me in a similar case

NSBundle * bundle = [NSBundle bundleForClass:[self class]];
NSString * file = [bundle pathForResource:@"my-cert" ofType:@"der"];; 
NSData * trustedCertData = [NSData dataWithContentsOfFile:[file stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSBundle bundleForClass: 方法为当前类的框架返回一个包.因此,如果您从 MyFramework 中调用此方法,则无需再次使用 MyFramework.framework 附加路径,但如果这是您的问题,您就会发现调试.

The NSBundle bundleForClass: method returns a bundle for the framework of the current class. So if you call this method from within MyFramework you won't need to append the path with MyFramework.framework again, but you would have caught that debugging if that was your problem.

此外,my-cert.der 中的破折号可能需要替换为 % 等效项.因此 stringByReplacingPercentEscapesUsingEncoding: 调用.

Also the dash in my-cert.der may need to be replaced with the % equivelent. hence the stringByReplacingPercentEscapesUsingEncoding: call.

这篇关于如何从主程序检索 iOS 框架包中的文件/资源​​?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆