1

アーカイブをダウンロードして Documents フォルダーに保存するためのコードは次のとおりです。

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:resource] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];

NSData *receivedData;
NSURLResponse *requestResponse;
NSError *requestError;

int i = 3;

while (i)    //Try 3 times
{
    receivedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&requestResponse error:&requestError];

    if (receivedData)
    {
        i = 0;
    }
    else i--;
}

if (!receivedData)
{
    NSLog(@"************************************************");
    NSLog(@"**  Error downloading data for resource : %@  **", resource);
    NSLog(@"************************************************");
}
else
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; //Get documents folder
    NSString *dataPath;
    dataPath = [documentsDirectory stringByAppendingPathComponent:LIST_FOLDER];
    NSError *documentError;

    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])    //Create folder
    {
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:YES attributes:nil error:&documentError];
    }

    NSLog(@"saveError = %@", documentError);   //Prints out null so no problem here

    NSError *writeError;

    [receivedData writeToFile:dataPath options:NSDataWritingAtomic error:&writeError];

    NSLog(@"write error = %@", writeError);

最後の nslog は以下を出力し、アーカイブを保存しません:

Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" "The operation couldn’t be completed. Is a directory"}

Documents ディレクトリにサブフォルダーが作成されますが、空です。

ありがとう

4

1 に答える 1

1

フォルダにデータを書き込もうとしています。フォルダ内のファイルに書き込む必要があります。

dataPath作成するディレクトリです。そのパスにファイル名を追加してから、ファイルをそのフルパスに (ファイル名を使用して) 書き込む必要があります。

于 2012-12-07T16:01:35.340 に答える