0

AFHTTPRequestOperationデバイスの Documents ディレクトリに大きなファイルをダウンロードするために使用しています。

NSURLRequest *request = [NSURLRequest requestWithURL:vectorFile.url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

NSString *path = [self pathForFileName:vectorFile.fileName];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {

    double percentDone = (double)totalBytesRead / (double)totalBytesExpectedToRead;
    progress(percentDone, totalBytesRead, totalBytesExpectedToRead);

}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

        NSString *path = [self pathForFileName:vectorFile.fileName];

        extracting();
        [SSZipArchive unzipFileAtPath:path toDestination:[self path] progressHandler:^(NSString *entry, unz_file_info zipInfo, long entryNumber, long total) {

            NSLog(@"Unzipping");

        } completionHandler:^(NSString *path2, BOOL succeeded, NSError *error) {

            if (!error) {
                NSLog(@"Successfully downloaded file to %@", path);


                dispatch_async(dispatch_get_main_queue(), ^{

                    completion(YES);

                });
            } else {

                NSLog(@"%@", error);
                failure(error);

            }


        }];

    });


} failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {

    NSLog(@"%@", error);
    failure(error);

}];
[operation start];

奇妙な理由で、シミュレーターでファイルをダウンロードすることはできますが、デバイス (iPhone 6) では次のエラーが発生します。

Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"

ブラウザとシミュレータで正しくダウンロードされるため、URL は正しいです。なぜこれがデバイス上で起こっているのでしょうか? 何が原因でしょうか?

デバイスの再起動とネットワーク設定のリセットを試みました。

4

2 に答える 2

1

ここでコメントを助けた後、宛先ディレクトリが作成されたことを最初に確認する必要があることがわかりました。

if(![[NSFileManager defaultManager] fileExistsAtPath:[self path]]) {
     [[NSFileManager defaultManager] createDirectoryAtPath:[self path] withIntermediateDirectories:YES attributes:nil error:NULL];
}

私はoutputStreamそれを私のために作成しただろうと思っていました。さらに奇妙なことは、シミュレーターで正しく機能したことです。

于 2016-07-27T23:52:05.793 に答える