Загрузка файлов из uiwebview в iphone sdk

Есть ли способ загрузить файл из UIWebView Я использую этот код на моем IBAction событии

- (IBAction)saveFile:(id)sender {
// Get the URL of the loaded ressource
NSURL *theRessourcesURL = [[self.webDisplay request] URL];
NSString *fileExtension = [theRessourcesURL pathExtension];

if ([fileExtension isEqualToString:@"png"] || [fileExtension isEqualToString:@"jpg"] || 
    [fileExtension isEqualToString:@"pdf"] || [fileExtension isEqualToString:@"html"]) {
    // Get the filename of the loaded ressource form the UIWebView's request URL
    NSString *filename = [theRessourcesURL lastPathComponent];
    NSLog(@"Filename: %@", filename);
    // Get the path to the App's Documents directory
    NSString *docPath = [self documentsDirectoryPath];
    // Combine the filename and the path to the documents dir into the full path
    NSString *pathToDownloadTo = [NSString stringWithFormat:@"%@/%@", docPath, filename];


    // Load the file from the remote server
    NSData *tmp = [NSData dataWithContentsOfURL:theRessourcesURL];
    // Save the loaded data if loaded successfully
    if (tmp != nil) {
        NSError *error = nil;
        // Write the contents of our tmp object into a file
        [tmp writeToFile:pathToDownloadTo options:NSDataWritingAtomic error:&error];
        if (error != nil) {
            NSLog(@"Failed to save the file: %@", [error description]);
        } else {
            // Display an UIAlertView that shows the users we saved the file :)
            UIAlertView *filenameAlert = [[UIAlertView alloc] initWithTitle:@"File saved" message:[NSString stringWithFormat:@"The file %@ has been saved.", filename] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [filenameAlert show];
            [filenameAlert release];
        }
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning" 
                                                        message:@"File could not be loaded" 
                                                       delegate:nil 
                                              cancelButtonTitle:@"Okay" 
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
        // File could notbe loaded -> handle errors
    }
} else {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning" 
                                                    message:@"File type not supported" 
                                                   delegate:nil 
                                          cancelButtonTitle:@"Okay" 
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];
    // File type not supported
}

} этот код открывает файл в UIWebView, который я хочу загрузить, и когда я нажимаю кнопку, открытый файл сохраняется. Но я хочу, чтобы мой UIWebView вел себя как обычный браузер, когда в нем появляется ссылка на скачивание и пользователь нажимает ее, UIWebView показывает диалог с опцией открыть или сохранить, если пользователь нажимает сохранить, файл сохраняется автоматически, а если нажать открыть, файл должен открыться в UIWebView.

5
задан Dharmesh Dhorajiya 21 February 2015 в 15:19
поделиться