オンライン サーバーからファイルをダウンロードしてアプリのローカル メモリに保存するアプリを作成しています。今、私がやりたいことは、「PDF を表示」ボタンをクリックすると、PDF ファイルが iBooks に直接開かれることです。
ファイルを保存するための私のコードは次のとおりです。
currentURL = @"http://weblink.com/folder1/folder2/file.pdf";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:currentURL]];
NSURLConnection *conn = [[NSURLConnection alloc] init];
(void)[conn initWithRequest:request delegate:self];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
data = [NSData dataWithContentsOfURL:[NSURL URLWithString:currentURL]];
dispatch_sync(dispatch_get_main_queue(), ^{
});
resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];
filePath = [resourceDocPath stringByAppendingPathComponent:@"myPDF.pdf"];
[data writeToFile:filePath atomically:YES];
});
そして、ファイルをiBooksに開くためにインターネットから見つけたものは次のとおりです(このコードをボタンクリックに配置しました):
NSString *path = [[NSBundle mainBundle] pathForResource:@"myPDF" ofType:@"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
UIDocumentInteractionController *docController = [[UIDocumentInteractionController alloc] init];
docController = [UIDocumentInteractionController interactionControllerWithURL:url];
docController.delegate = self;
そのコードと次のメッセージでアプリがクラッシュしています。
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'