因此,我正在尝试读取一个简单的epub文件,它已经下载到我的XCode项目中,因此应该能够从我的mainBundle中读取,对吧?
使用以下代码:
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]]];我得到以下错误:
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 请帮帮我,提前谢谢。
发布于 2015-03-26 18:04:43
看起来你是在你的应用中下载文件,而不是在Xcode中。您可以在此处下载该文件:
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.feedbooks.com/book/3471.epub"]];因为mainBundle是只读的,所以您想要将其保存到mainBundle目录中:
NSString *doucmentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) firstObject];
NSString *filePath = [doucmentPath stringByAppendingPathComponent:@"3471.epub"];
NSLog(@"filePath iss....%@",filePath);
[detailViewController loadEpub:[NSURL fileURLWithPath:filePath];或者使用带有错误检查的更现代的NSURL方法:
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);
}https://stackoverflow.com/questions/29275492
复制相似问题