Read local files by loading UIWebView

Read local files by loading UIWebView

      UIWebView is Apple's own framework, can be considered an internal Apple program browser, you can browse web pages, it can also open text files HTML / HTM, PDF, docx, txt and other formats, in fact, Apple's own Safari browser is to use UIWebView to achieve, specific principles simply transfer data into the server MIME is an identifier or the like, which then tells the browser to use plug-in to read the relevant corresponding file.

A, UIWebView to load local files by various methods loadRequest

       Examples show:

      (A) UIWebView to load local files loadRequest method:

       1, first of all need to show the text into a word document inside, and then after you save the document content, the word document directly to the project dragged inside;

       2, and then you need to show the contents of control word inside, initialize a webview, then use the method to load loadRequest word document can be.

      (B) UIWebView to load local files loadRequest method:

         1, first word content on a test server above, and then copied out the link;

         2, and then you need to show the contents of control word inside, initialize a webview, then use the method to load loadRequest word document can be.

   NSURL *url = [NSURL URLWithString:@"http://test.tea.com.cn:88/static/upload/使用说明.doc"];
    [webView loadRequest:[NSURLRequest requestWithURL:url]];
    webView.delegate = self;
    NSData *data = [[NSData alloc] initWithContentsOfURL:url];

    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];

    [self.view addSubview:webView];

Two, UIWebView to load local files by various methods loadData

      1, load docx file:

    
NSString *path = [[NSBundle mainBundle] pathForResource:@"使用说明.docx" ofType:nil];

NSURL *url = [NSURL fileURLWithPath:path];
NSData *data = [NSData dataWithContentsOfFile:path];

[self.webView loadData:data MIMEType:@"application/vnd.openxmlformats-officedocument.wordprocessingml.document" textEncodingName:@"UTF-8" baseURL:nil];

 

     2, load the pdf file:

     NSString *path = [[NSBundle mainBundle] pathForResource:@"使用说明.pdf" ofType:nil];       NSURL *url = [NSURL fileURLWithPath:path];       NSData *data = [NSData dataWithContentsOfFile:path];       [self.webView loadData:data MIMEType:@"application/pdf" textEncodingName:@"UTF-8" baseURL:nil];  

 

     3, load the txt file:

  
NSString *path = [[NSBundle mainBundle] pathForResource:@"使用说明.txt" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSData *data = [NSData dataWithContentsOfFile:path];
[self.webView loadData:data MIMEType:@"text/plain" textEncodingName:@"UTF-8" baseURL:nil];

 

     4, load the html file:

    NSString *path = [[NSBundle mainBundle] pathForResource:@"使用说明.html" ofType:nil];      
NSURL *url = [NSURL fileURLWithPath:path];
NSData *data = [NSData dataWithContentsOfFile:path];
[self.webView loadData:data MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:nil];

 

     5, get MIMEType type specified URL

- (NSString *) mimeType: (NSURL * ) url {  
        
NSURLRequest
* Request = [NSURLRequest requestWithURL: url]; // 2.NSURLConnection
// 3. In NSURLResponse, the server what browser to use to open the file, use the synchronization method to MIMEType NSURLResponse * Response = nil;
[NSURLConnection sendSynchronousRequest: Request returningResponse: & Response error: nil];

return response.MIMEType;

 

Third, the summary

UIWebView loading content in three ways:
1, loading local data files, MIMEType specified file, using the coding format @ "UTF-. 8";
2, loading html string (may be loaded all or part of an html file);
3, loading NSURLRequest files (the same as the first two NSURLConnect).

Guess you like

Origin www.cnblogs.com/OIMM/p/11018294.html