2011-11-10 16 views
5

वहाँ फ़ाइलडाउनलोड कर रहा है फ़ाइलों

- (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 में फ़ाइल खोलने, जो मैं डाउनलोड करना चाहते मेरी IBAction घटना पर UIWebView मैं इस कोड का उपयोग कर रहा से डाउनलोड करने के लिए कोई तरीका है और जब मैं बटन दबाता हूं तो खुली फ़ाइल सहेज जाती है। लेकिन मैं सामान्य ब्राउज़र की तरह व्यवहार करने के लिए अपना UIWebView चाहता हूं, जब डाउनलोड लिंक इसमें दिखाई देता है और उपयोगकर्ता इसे दबाता है, UIWebView विकल्प खोलने के साथ संवाद दिखाएं या इसे सहेज लें यदि उपयोगकर्ता फ़ाइल को सहेजने के लिए फ़ाइल को स्वचालित रूप से सहेज लेता है और यदि उपयोगकर्ता इसे फ़ाइल खोलता है UIWebView में खोलना चाहिए।

उत्तर

6

आप में webView:shouldStartLoadWithRequest प्रदान कर सकते हैं अपने UIWebViewDelegate इसलिए हर बार जब उपयोगकर्ता के बारे में एक और वेब पेज के लिए स्थानांतरित करने के लिए है, आप लिंक कैसा दिखाई देता है की जाँच करने का मौका:

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

आपके उत्तर के लिए धन्यवाद सर्जियो और आपकी प्रतिक्रिया के लिए आपकी टिप्पणी की गई लाइन मुझे बहुत गाइड करती है :) –

+0

@ सर्जियो अगर मैं एक फ़ाइल डाउनलोड करने का प्रयास करता हूं (याहू/एक्सचेंज सर्वर से हो सकता है) तो इसमें ** बोल्ड ** https ** बोल्ड ** और योजना होगी पथ एक्सटेंशन कुछ उदाहरण के लिए यादृच्छिक स्ट्रिंग है। एशक्स इत्यादि ...! मैं ऐसी फाइलों के डाउनलोड को कैसे संकेत दूं? इस पर कोई विचार है? – Nirav

संबंधित मुद्दे