【发布时间】:2011-06-05 21:58:54
【问题描述】:
我正在尝试开发一个下载管理器。我现在几乎可以在 linkclick 上的任何地方下载文件。
在 - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType 我检查 url 是否是二进制文件(如 zipfile)的 url。比我设置一个 nsurlconnection
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:20.0];
[urlRequest setValue:@"User-Agent" forHTTPHeaderField:@"Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en) AppleWebKit/418.9 (KHTML, like Gecko) Safari/419.3"];
NSURLConnection *mainConnection = [NSURLConnection connectionWithRequest:urlRequest delegate:self];
if (nil == mainConnection) {
NSLog(@"Could not create the NSURLConnection object");
}
-
(void)connection:(NSURLConnection )connection didReceiveResponse:(NSURLResponse)response {
self.tabBarController.selectedIndex=1;
[receivedData setLength:0];
percent = 0;
localFilename = [[[url2 absoluteString] lastPathComponent] copy];
NSLog(localFilename);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0] ;
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:localFilename];
[[NSFileManager defaultManager] createFileAtPath:appFile contents:nil attributes:nil];
[downloadname setHidden:NO];
[downloadname setText:localFilename];
expectedBytes = [response expectedContentLength];
exp = [response expectedContentLength];
NSLog(@"content-length: %lli Bytes", expectedBytes);
file = [[NSFileHandle fileHandleForUpdatingAtPath:appFile] retain];
if (file) {
[file seekToEndOfFile];
}
}
(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
if (file) {
[file seekToEndOfFile];
}
[file writeData:data];
[receivedData appendData:data];
long long resourceLength = [receivedData length];
float res = [receivedData length];
percent = res/exp;
[progress setHidden:NO];
[progress setProgress:percent];
NSLog(@"Remaining: %lli KB", (expectedBytes-resourceLength)/1024);
[kbleft setHidden:NO];
[kbleft setText:[NSString stringWithFormat:@"%lli / %lli KB", expectedBytes/1024 ,(resourceLength)/1024]];
}
在连接完成加载中,我关闭了文件。几乎所有主机都可以正常工作,除了主机之前有一个捕获程序,比如filedude.com 在 uiwebview 我可以浏览到下载页面输入验证码并获取下载链接。当我单击它时,将在带有文件名的文档目录中创建文件并开始下载,但他没有得到任何数据。每个文件都有 0kb 和 NSLog(@"content-length: %lli Bytes", expectedBytes);发出类似 100-400 字节的内容。
谁能帮我解决这个问题?
亲切的问候
【问题讨论】:
标签: iphone objective-c iphone-sdk-3.0