0

オンラインサーバーからpdfファイルをダウンロードするアプリを作ろうとしています。私がしたことは、3 つの異なる URL を持つ 3 つのボタンを作成し、アプリのサンドボックスに保存して iBooks で開くことでした。

しかし、アプリがクラッシュし、このエラーが発生しています...

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter' ***

これが私のコードです:ボタンクリック時:

     if (sender.tag == 1) {
        pdfTag = 1;
        pdfSource1 = @"http://myweb.com/folder/folderagain/file1.pdf";
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:pdfSource1]];
        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, ^{
            dataSource1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:pdfSource1]];
            dispatch_sync(dispatch_get_main_queue(), ^{
            });
            pdfSourcePath1 = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"myPDF1.pdf"];
            NSLog(@"PDF path: %@",pdfSourcePath1);
            [dataSource1 writeToFile:pdfSourcePath1 atomically:YES];
        });

    } else if (sender.tag == 2) {
        pdfTag = 2;
        pdfSource2 = @"http://myweb.com/folder/folderagain/file2.pdf";
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:pdfSource2]];
        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, ^{
            dataSource2 = [NSData dataWithContentsOfURL:[NSURL URLWithString:pdfSource2]];
            dispatch_sync(dispatch_get_main_queue(), ^{
            });
            pdfSourcePath2 = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"myPDF2.pdf"];
            NSLog(@"PDF path: %@",pdfSourcePath2);
            [dataSource2 writeToFile:pdfSourcePath2 atomically:YES];
        });
    } else if (sender.tag == 3) {
        pdfTag = 3;
        pdfSource3 = @"http://myweb.com/folder/folderagain/file3.pdf";
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:pdfSource3]];
        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, ^{
            dataSource3 = [NSData dataWithContentsOfURL:[NSURL URLWithString:pdfSource3]];
            dispatch_sync(dispatch_get_main_queue(), ^{
            });
            pdfSourcePath3 = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"myPDF3.pdf"];
            NSLog(@"PDF path: %@",pdfSourcePath3);
            [dataSource3 writeToFile:pdfSourcePath3 atomically:YES];
        });
    }

ファイルを開くには:

     if (pdfTag == 1) {
     NSURL *url = [NSURL fileURLWithPath:pdfSourcePath1];
     docController1 = [UIDocumentInteractionController interactionControllerWithURL:url];

     docController1.delegate = self;

     [docController1 presentOpenInMenuFromRect:btn1.frame inView:self.view animated:YES];
    } else if (pdfTag == 2) {
        NSURL *url = [NSURL fileURLWithPath:pdfSourcePath2];
        docController2 = [UIDocumentInteractionController interactionControllerWithURL:url];

        docController2.delegate = self;

        [docController2 presentOpenInMenuFromRect:btn2.frame inView:self.view animated:YES];
    } else if (pdfTag == 3) {
        NSURL *url = [NSURL fileURLWithPath:pdfSourcePath3];
        docController2 = [UIDocumentInteractionController interactionControllerWithURL:url];

        docController2.delegate = self;

        [docController2 presentOpenInMenuFromRect:btn3.frame inView:self.view animated:YES];
    }

答えが見つかることを願っています。

4

3 に答える 3

0

initFileURLWithPath あなたのコードでは、どこでも使用した API を確認できませんでした..

以下は、あなたのタスクに対する私の提案です。

以下は、サーバー ファイル パスを使用してリモート Pdf を取得しNSData、アプリケーション サンドボックスに保存するコードです。

     NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:bookUrlString]];
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *documentsDirectory = [paths objectAtIndex:0];
     NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"myPDF.pdf"];
     [pdfData writeToFile:filePath atomically:YES

この目的に関する私の理解によれば、これには何も必要ありませNSURLRequestNSURLConnection...

于 2013-05-17T05:00:38.287 に答える
0

NSData dataWithContentsOfURLファイルのサイズが小さい限り、うまく機能します。

この方法を使用して大きなファイルをダウンロードすることはお勧めしません。代わりにNSURLConnectionまたは を使用してAFNetworkingください。

ファイルの使用方法NSURLConnectionAFNetworkingダウンロード方法は、次の回答で説明されています: https://stackoverflow.com/a/16454923/2548707

于 2013-07-04T01:27:37.557 に答える