将文件下载到iphone中的应用程序目录

将文件下载到iphone中的应用程序目录,iphone,ipad,uiwebview,nsdata,writetofile,Iphone,Ipad,Uiwebview,Nsdata,Writetofile,我不熟悉iPhone 我目前正在开发一款iPhone应用程序,希望实现从互联网下载文件的功能。我已经创建了UIWebView,但我想知道在webview中链接文件并将其下载到documents目录中指定文件夹时捕获文件的最佳方法 这是我的代码片段 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [self.fileData setLength

我不熟悉iPhone

我目前正在开发一款iPhone应用程序,希望实现从互联网下载文件的功能。我已经创建了
UIWebView
,但我想知道在webview中链接文件并将其下载到documents目录中指定文件夹时捕获文件的最佳方法

这是我的代码片段

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

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

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection
 {
     [activityIndicator stopAnimating];
     activityIndicator.hidden=TRUE;
}

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

    url = [request URL];

    //CAPTURE USER LINK-CLICK.
    NSString *file = [NSString stringWithString:[url absoluteString]];;
    NSURL *fileURL = [NSURL URLWithString:file];
    NSURLRequest *req = [NSURLRequest requestWithURL:fileURL];
    [NSURLConnection connectionWithRequest:req delegate:self];
     data = [NSData dataWithContentsOfURL:url];

            //Saving file at downloaded path.

        DirPath = [DestPath stringByAppendingPathComponent:[url lastPathComponent]];
        [data writeToFile:DirPath atomically:YES];

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



    return YES;   
}

问题是在何处写入条件,如果我的下载失败,并且我在日志中收到警告,显示:
“wait\u fences:failed to receive reply:10004003”

似乎您在这段代码中多次执行相同的操作。例如,您创建了一个新的NSURLRequest对象,即使一个对象已经在委托方法内部传递了?在创建新的NSURLConnection之后,是否还运行SynchronousDataWithContentsOfURL方法?您还将一些数据附加到属性中,但却不处理该属性


您可能希望在加载UIWebView时创建一个新的异步NSURLConnection。从那里,允许UIWebView加载该页面。在委托方法内部,将下载的数据附加到文件,而不是将数据附加到某个属性。连接完成下载后,向用户发出警报,通知用户数据已下载并保存。

您知道任何示例教程吗?我已向您明确说明了需要执行的操作。在大多数情况下,您已经在发布的代码中编写了它。