【发布时间】:2012-08-18 16:45:42
【问题描述】:
我是 iPhone 新手,
我目前正在开发一个 iPhone 应用程序,并希望实现从 Internet 下载文件的功能。我已经创建了UIWebView,但想知道在 webview 中链接到文件时捕获文件的最佳方法,然后将它们下载到文档目录中的指定文件夹。
这是我的代码 sn-p,
- (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_fences: failed to receive reply: 10004003”
【问题讨论】:
标签: iphone ipad uiwebview nsdata writetofile