0

当用户单击下载链接时,我的应用程序使用 UIWebView 加载网站并下载文件,应用程序冻结直到文件完成下载,然后应用程序恢复正常,它不会崩溃,它只是冻结.. 我该如何解决这?

我使用此代码下载

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

if(navigationType == UIWebViewNavigationTypeLinkClicked) 
{
    NSURL *requestedURL = [request URL];

    // ...Check if the URL points to a file you're looking for...
    // Then load the file
    NSData *fileData = [[[NSData alloc] initWithContentsOfURL:requestedURL] autorelease];

    // Get the path to the App's Documents directory
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
    [fileData writeToFile:[NSString stringWithFormat:@"%@/%@", documentsDirectory, [requestedURL lastPathComponent]] atomically:YES];
} 
return YES;
}


- (NSString *)documentsDirectoryPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
return documentsDirectoryPath;
}
4

1 回答 1

0

请参阅 Apple iOS 5.1 文档中的“URL 加载系统编程指南”。您需要设置一个异步请求。URL 加载系统有许多代码示例。

于 2012-05-25T01:14:19.073 回答