3

オンライン サーバーからファイルをダウンロードしてアプリのローカル メモリに保存するアプリを作成しています。今、私がやりたいことは、「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'
4

3 に答える 3

0

PDFをドキュメントフォルダーに保存するには、これを試してください

- (NSString *)applicationDocumentsDirectory
{
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

//ブロック内

filePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"myPDF.pdf"];
NSLog(@"PDF path: %@",filePath);
[data writeToFile:filePath atomically:YES];

また、iBooks ユーザー UIDocumentInteractionController で pdf を開く方法については、こちらのチュートリアルを参照してください。

于 2013-05-14T04:09:13.483 に答える
0
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"itms-bookss://%@",self.pathToFile]]];

これにより、ローカルの PDF が Ibooks に開かれます。Ibooks アプリにすでにある場合は、「itms-books」ではなく「itms-bookss」。

それ以外の場合、ファイルを iBooks に追加するように見えるのは、あまりカスタマイズできない UIDocumentInteractionController を使用することです。

于 2015-06-19T15:20:47.973 に答える