2011-11-10 26 views
5

Có cách nào để tải về tập tin từ UIWebView tôi đang sử dụng mã này vào sự kiện IBAction tôiTải tập tin từ UIWebView trong iphone sdk

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

} mã này mở tập tin trong UIWebView, mà tôi muốn tải về và khi tôi nhấn nút, tập tin đã mở sẽ được lưu. Nhưng tôi muốn UIWebView hoạt động giống như trình duyệt thông thường, khi liên kết tải xuống xuất hiện trong đó và người dùng nhấn nó, UIWebView hiển thị hộp thoại với tùy chọn mở hoặc lưu nó nếu người dùng nhấn lưu tệp sẽ lưu tự động và nếu người dùng nhấn mở tệp nên mở trong UIWebView.

Trả lời

6

Bạn có thể cung cấp webView:shouldStartLoadWithRequest trong bạn UIWebViewDelegate để mỗi khi người dùng sắp di chuyển đến một trang web, bạn có cơ hội để kiểm tra những gì liên kết trông giống như:

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

Cảm ơn bạn Sergio cho câu trả lời của bạn và cho phản ứng của bạn dòng nhận xét của bạn hướng dẫn cho tôi rất nhiều :) –

+0

@Sergio Nếu tôi cố gắng tải xuống một tệp (có thể từ máy chủ yahoo/exchange), nó sẽ có lược đồ là ** bold ** https ** bold ** và pathExtension là một chuỗi ngẫu nhiên cho ví dụ. ashx vv ...! Làm cách nào để nhắc tải xuống các tệp như vậy! Bất kỳ ý tưởng về điều này? – Nirav

Các vấn đề liên quan