0

私はiPhoneを初めて使用しますが、

私は現在iPhoneアプリを開発しており、URLからファイルをダウンロードする機能を実装したいと考えています。を作成しました。ウェブビューのリンクをUIWebViewクリックするdownloadとダウンロードが開始され、そのファイルがドキュメントディレクトリの指定されたフォルダに保存されます。しかし、ダウンロードしたファイルを見ることができません。

これが私のコードスニペットです、

//CAPTURE USER LINK-CLICK in UIwebView.

 - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {

         // Create the request.
            NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:DUrl]
                                                      cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                  timeoutInterval:60.0];

            // create the connection with the request and start loading the data
            NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
            if (theConnection) {
                // Create the NSMutableData to hold the received data.
                receivedData = [[NSMutableData data] retain];
            } else {
                NSLog(@"Inform the user that the connection failed."); 
            }
  return YES; 
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data1
{
    [receivedData appendData:data1];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);

    DirPath=[self MyApplicationDocumentDirectory];
    [receivedData writeToFile:DirPath atomically:YES];

    UIAlertView* Alert = [[UIAlertView alloc] initWithTitle:@"Download Complete !"
                                                         message:nil delegate:nil 
                                               cancelButtonTitle:@"OK"
                                               otherButtonTitles:nil];
    [Alert show];
    [Alert release];


    // release the connection, and the data object
    [connection release];
    [receivedData release];
}

どんな助けでも適用されます。

編集:

            BOOL success =[[NSFileManager defaultManager] fileExistsAtPath:MyDirPath];

            if (success) 
            {
                    UIAlertView* innerAlert = [[UIAlertView alloc] initWithTitle:@"Already downloaded."
                                                                         message:@"Do you want to Downlaod again ?" delegate:self 
                                                               cancelButtonTitle:nil
                                                               otherButtonTitles:@"Yes",@"No",nil];
                    [innerAlert show];
                    [innerAlert release];
            }

この条件をどこに書くか?

4

1 に答える 1

2

編集次のようにダウンロードしたデータを書き込む(保存する)前に、ダウンロードしたファイルがdocdirにすでに存在するかどうかを確認します。

 NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:DirPath error:nil];
 BOOL fileExists = NO;
 for(NSString *fileName in dirContents)
 {
   NSString *filePath = [DirPath stringByAppendingPathComponent:fileName];
   NSData *fileData = [NSData dataWithContentsOfFile:filePath];
    if([receivedData isEqualToData:fileData]) //your receivedData here
    {
        fileExists = YES;
    }
 }
 if(fileExists)
 {
   NSLog(@"File exists");
 }
 else
 {
   NSLog(@"File  does not exists");
  }

データを書き込むためのfileNameを指定するのを忘れました:

DirPath=[self MyApplicationDocumentDirectory];
  NSString *filePath = [DirPath stringByAppendingPathComponent:@"yourFileName"];
[receivedData writeToFile:filePath atomically:YES];
于 2012-08-20T10:29:54.933 に答える