0

我正在尝试制作一个从在线服务器下载 pdf 文件的应用程序。我所做的是创建了 3 个具有 3 个不同 URL 的按钮,并将其保存到应用程序的沙箱并在 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。

下面是我对你的任务的建议,

下面是NSData使用服务器文件路径获取远程 Pdf 并存储在应用程序沙箱中的代码。

     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

网络是一项复杂的任务,我绝不建议您使用该-initWithURL方法下载或上传文件,因为它不是异步的并且会阻塞主线程。您应该使用NSURLConnection类及其委托方法,或者只接受队列和完成块的惊人类方法。
你正在做一些奇怪的事情,你使用 NSURLConnection 但你使用了 ..WithURL 方法,你应该使用其中的一个。
网络上有不同的下载管理器可以在整个过程中为您提供帮助,因为它们包装了 NSURLConnection 类和委托方法。
有:

  1. AF网络链接
  2. MKNetworking(我最喜欢的)链接
于 2013-05-17T06:13:45.387 回答
0

NSData dataWithContentsOfURL只要文件的大小很小,它就可以很好地工作。

我不建议您使用这种方法下载大文件,而是使用NSURLConnectionor AFNetworking

以下答案解释了 如何使用NSURLConnection和 下载文件: https ://stackoverflow.com/a/16454923/2548707AFNetworking

于 2013-07-04T01:27:37.557 回答