0

我正在嘗試製作一個從在線服務器下載pdf文件的應用程序。 我做的是我創建了3個按鈕,它有3個不同的URL並將其保存到應用程序的沙箱並在iBooks中打開它。目標C:多個下載

但它崩潰的應用程序,我有這個錯誤說...

*** 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]; 
    } 

我希望我能找到答案。

+0

據我'pdfSourcePath1,pdfSourcePath2,pdfSourcePath3'是'nil'。在塊源代碼中設置pdf源路徑之前打開文件方法。嘗試放置調試點。 –

+0

嘗試打開文件或下載時發生錯誤嗎?你可以在建立NSURL打開之前記錄pdfSourcePath1,2,3。最後,您直接使用GCD來執行請求很奇怪,難以閱讀。查找NSURLConnection sendAsynchronousRequest:.... – danh

+0

@Deepesh我的代碼打開文件是在connectionDidFinishLoading:(NSURLConnection *)連接 –

回答

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 ...

+0

代碼通過使用initWithContentsOfURL(可能不太合適)間接使用NSURLRequest和NSURLConnection。如果OP想要發佈應用程序,幾乎肯定會使用異步請求從Web服務中獲取PDF。 – danh

0

網絡是一項複雜的工作,我絕不會建議你下載或使用上傳文件-initWithURL方法,因爲它不是異步的並且會阻塞主線程。你應該使用NSURLConnection類和它的委託方法,或只接受一個隊列和一個完成塊的驚人的類方法。
你正在做怪異的東西,你使用NSURLConnection但你使用..WithURL方法,你應該去與他們一個。
網絡上有不同的下載管理器可以幫助你完成整個過程,因爲它們包裝NSURLConnection類和委託方法。
有:

  1. AFNetworking link
  2. MKNetworking(我的最愛)link
0

NSData dataWithContentsOfURL作品長以及文件的大小小。

我不會建議你使用這種方法來下載大文件,而是使用NSURLConnectionAFNetworking

如何使用NSURLConnectionAFNetworking下載下面的答案解釋文件: https://stackoverflow.com/a/16454923/2548707

+0

'dataWithContentsOfURL:'不應該用於檢索整個網絡中的信息,不管金額多少。它阻塞了線程;任何網絡問題都有可能導致應用程序停滯。 –

+0

@JoshCaswell的確如此。 – SahilS