2011-11-10 13 views
5

Esiste un modo per scaricare file da UIWebView Sto usando questo codice sul mio evento IBActiondownload di file da UIWebView nel SDK iPhone

- (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 
} 

} questo codice aprire il file in UIWebView, che voglio scaricare e quando preme il pulsante il file aperto viene salvato. Ma voglio che il mio UIWebView si comporti come un normale browser, quando il link di download appare in esso e l'utente lo preme, UIWebView visualizza finestra di dialogo con l'opzione apri o salva se l'utente preme salva il file viene salvato automaticamente e se l'utente clicca apri il file dovrebbe aprirsi in UIWebView.

risposta

6

È possibile fornire webView:shouldStartLoadWithRequest nel vostro UIWebViewDelegate in modo che ogni volta che l'utente è in procinto di passare a un'altra pagina web, si ha la possibilità di controllare ciò che il link appare come:

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

    if ([[[request URL] scheme] isEqual:@"http"] && 
     [[[request URL] pathExtension]...]) 
      <your download/save code here> 
      return NO; //-- no need to follow the link 
    } 
    return YES; //-- otherwise, follow the link 
    } 
+1

Grazie Sergio per la risposta e per la tua risposta la tua linea commentata mi guida molto :) –

+0

@Sergio Se provo a scaricare un file (potrebbe provenire da un server yahoo/exchange) avrà schema come ** bold ** https ** bold ** e il pathExtension è una stringa casuale per es. cenere ecc ...! Come posso richiedere il download di tali file! Qualche idea su questo? – Nirav

Problemi correlati