0

Web リンクから複数の PDF ファイルをダウンロードできません。私が持っているコードは Web サーバー上のファイルを見つけることができますが、約 360 バイトしかダウンロードしません。各ファイルは約 2 ~ 3 MB で、これを読み取ると %PDF が生成されます。私の理解では、これは PDF の最初のバイトです。

このコードで何が起こるかというと、PDF ファイルを含む同じ Web サーバーからダウンロードされたばかりの plist ファイルから辞書にアクセスする文字列が渡されます。文字列は、Web サーバー上にある PDF の実際の名前を取得し、NSURLConnection を介して NSData オブジェクトにダウンロードされます。最後に、Apps Documents ディレクトリに書き込まれます。

どんな助けでも素晴らしいでしょう!

-(void)fileToDownload:(NSString *)stringToPDFName {

NSString *downloadfile = [[masterDictionary objectForKey:@"PDF Dictionary"] objectForKey:stringToPDFName];
NSLog(@"downloadFile: %@",downloadfile);

NSString *pathToFile = [NSString stringWithFormat:@"%@%@.pdf",staticURLToPDF,downloadfile];
NSLog(@"pathToFile: %@",pathToFile);
NSError *error;

if ([listOfPDFsAlreadyDownloaded containsObject:pathToFile]) {
    NSLog(@"File Already Downloaded From New List: %@", [pathToFile stringByReplacingOccurrencesOfString:staticURLToPDF withString:@""]);
} else {
    //download new file
    NSString *urlString = [[staticURLToPDF stringByAppendingPathComponent:downloadfile] stringByAppendingPathExtension:@".pdf"];

    NSData *pdfDownloading = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] returningResponse:NULL error:&error];


    NSLog(@"pdfDownloading Data: %@, Error: %@", pdfDownloading,error);

    BOOL written = [pdfDownloading writeToFile:[NSString stringWithFormat:@"%@/POGs/%@.pdf",documentsDirectory,downloadfile] atomically:YES];

    NSLog(@"FileDownloading %@/POGs/%@.pdf",documentsDirectory,downloadfile);

    if (written) {
        NSLog(@"Success");
        downloadedDocuments++;
    } else {
        NSLog(@"File Didn't Save");
    }
    [listOfPDFsAlreadyDownloaded addObject:pathToFile];
    NSLog(@"File to Download: %@",[pathToFile stringByReplacingOccurrencesOfString:staticURLToPDF withString:@""]);
    NSLog(@"%i documentsDownloaded",downloadedDocuments);
}

}

4

1 に答える 1

0

NSURLSession特にNSURLSessionDownloadTaskファイルをダウンロードするために使用したい。

Apple の公式ドキュメントから始めることをお勧めします: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/UsingNSURLSession.html

また、同期リクエストは行わないでください。ネットワークは本質的に低速であるため、タスクは常に非同期で実行する必要があります。

于 2016-05-26T08:47:08.090 に答える