首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法从mainBundle读取ePub文件

无法从mainBundle读取ePub文件
EN

Stack Overflow用户
提问于 2015-03-26 17:57:19
回答 1查看 118关注 0票数 0

因此,我正在尝试读取一个简单的epub文件,它已经下载到我的XCode项目中,因此应该能够从我的mainBundle中读取,对吧?

使用以下代码:

代码语言:javascript
复制
  NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.feedbooks.com/book/3471.epub"]];

    //Store the Data locally as epub  File if u want pdf change the file extension

    NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];

    NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"3471.epub"];
     [pdfData writeToFile:filePath atomically:YES];
    NSLog(@"filePath iss....%@",filePath);
     [detailViewController loadEpub:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:filePath]]];

我得到以下错误:

代码语言:javascript
复制
2015-03-26 15:14:31.301 AePubReader[6742:142694] filePath iss..../Users/pavan/Library/Developer/CoreSimulator/Devices/10E8D95D-A8A3-44B4-BE83-B53701A6B8E6/data/Containers/Bundle/Application/A07A5926-ED20-4D79-8F37-3D254080C1D8/Documents/3471.epub

2015-03-26 15:14:31.302 AePubReader[6742:142694] -[NSBundle pathForResource:]: unrecognized selector sent to instance 0x7facb9629590

2015-03-26 15:14:31.346 AePubReader[6742:142694] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSBundle pathForResource:]: unrecognized selector sent to instance 0x7facb9629590'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010f400a75 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010e734bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010f407d1d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010f35f9dc ___forwarding___ + 988
    4   CoreFoundation                      0x000000010f35f578 _CF_forwarding_prep_0 + 120
    5   AePubReader                         0x000000010c6d293c -[AePubReaderAppDelegate 

请帮帮我,提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2015-03-26 18:04:43

看起来你是在你的应用中下载文件,而不是在Xcode中。您可以在此处下载该文件:

代码语言:javascript
复制
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.feedbooks.com/book/3471.epub"]];

因为mainBundle是只读的,所以您想要将其保存到mainBundle目录中:

代码语言:javascript
复制
NSString *doucmentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) firstObject];
NSString *filePath = [doucmentPath stringByAppendingPathComponent:@"3471.epub"];
NSLog(@"filePath iss....%@",filePath);
[detailViewController loadEpub:[NSURL fileURLWithPath:filePath];

或者使用带有错误检查的更现代的NSURL方法:

代码语言:javascript
复制
NSError *error = nil;
NSURL *documentURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:&error];

if (documentURL) {
    NSLog(@"Failed to get document directory: %@", error);
    return;
}
NSURL *fileURL = [documentURL URLByAppendingPathComponent:@"3471.ePub"];

if (![pdfData writeToURL:fileURL options:NSDataWritingAtomic error:&error] ) {
    NSLog(@"Failed to write to path %@ with error : %@", fileURL, error);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29275492

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档