iOS elearning------4 UIWebView Three ways to load local data

Source: Internet
Author: User

UIWebView is an iOS built-in browser that can browse Web pages, open documents html/htm PDF Docx txt, and other formats. The Safari browser is done through UIWebView.

The server tells the browser to use that plug-in to read the relevant files by putting the MIME identifiers into the transmitted data.


UIWebView loading various local files (via the LoadData method):

-(void) viewdidload{[Super Viewdidload];        [Self setupui];    NSString *path = [[NSBundle mainbundle] pathforresource:@ "about. docx" Oftype:nil];    Nsurl *url = [Nsurl Fileurlwithpath:path];            NSLog (@ "%@", [self mimetype:url]); WebView load the local file, you can use the way to load the data//The first argument is a nsdata, local file corresponding data//the second parameter is mimetype//The third parameter is the encoding format//relative address, generally loading local file is not used, you can refer to the        Find the relevant file in the BaseURL.        Loads the files in the sandbox as binary data, nsdata *data = [NSData Datawithcontentsoffile:path]; [Self.webview loaddata:data mimetype:@ "Application/vnd.openxmlformats-officedocument.wordprocessingml.document" textencodingname:@ "UTF-8" Baseurl:nil];} #pragma mark load docx file-(void) loaddocx{nsstring *path = [[NSBundle mainbundle] pathforresource:@ "about. docx" Oftype:ni    L];    Nsurl *url = [Nsurl Fileurlwithpath:path];    NSLog (@ "%@", [self mimetype:url]);        NSData *data = [NSData Datawithcontentsoffile:path]; [Self.webview loaddata:data mimetype:@ "Application/vnd.openxmlformats-officedocument.wordprocessinGml.document "textencodingname:@" UTF-8 "Baseurl:nil";} #pragma mark load PDF file-(void) loadpdf{nsstring *path = [[NSBundle mainbundle] pathforresource:@ "Ios6cookbook.pdf" OfType    : nil];    Nsurl *url = [Nsurl Fileurlwithpath:path];        NSLog (@ "%@", [self mimetype:url]);        NSData *data = [NSData Datawithcontentsoffile:path]; [Self.webview loaddata:data mimetype:@ "application/pdf" textencodingname:@ "UTF-8" Baseurl:nil];}    #pragma mark loads the local text file-(void) loadtext{nsstring *path = [[NSBundle mainbundle] pathforresource:@ "about. txt" oftype:nil];    Nsurl *url = [Nsurl Fileurlwithpath:path];    NSLog (@ "%@", [self mimetype:url]);        NSData *data = [NSData Datawithcontentsoffile:path]; [Self.webview loaddata:data mimetype:@ "Text/plain" textencodingname:@ "UTF-8" Baseurl:nil];} #pragma mark loads the local HTML file-(void) loadhtml{nsstring *path = [[NSBundle mainbundle] pathforresource:@ "demo.html" Oftype:ni    L];    Nsurl *url = [Nsurl Fileurlwithpath:path];  NSLog (@ "%@", [self mimetype:url]);      NSData *data = [NSData Datawithcontentsoffile:path]; [Self.webview loaddata:data mimetype:@ "text/html" textencodingname:@ "UTF-8" Baseurl:nil];} #pragma mark gets the MimeType type of the specified URL-(NSString *) MimeType: (Nsurl *) url{//1nsurlrequest nsurlrequest *request = [Nsurlre    Quest Requestwithurl:url];        2NSURLConnection//3 in Nsurlresponse, the server tells the browser how to open the file.    After using the synchronous method, go to mimetype nsurlresponse *response = nil;    [Nsurlconnection sendsynchronousrequest:request returningresponse:&response Error:nil]; return response. MIMEType;}


UIWebView loading various local files (via the Loadrequest method):

-(void) viewdidload{[Super Viewdidload];            [Self setupui];  The first way to load a path//nsstring *path = [[NSBundle mainbundle] pathforresource:@ "about. docx" oftype:nil];//nsurl *url = [Nsurl    Fileurlwithpath:path];        The second way to load a path is nsurl *url = [[NSBundle mainbundle] urlforresource:@ "ios6cookbook.pdf" withextension:nil]; UIWebView the second way to load a file.    The first method is in the comments below.    Nsurlrequest *request = [Nsurlrequest Requestwithurl:url];        [Self.webview Loadrequest:request];    NSLog (@ "%@", [self mimetype:url]); WebView load the local file, you can use the way to load the data//The first argument is a nsdata, local file corresponding data//the second parameter is mimetype//The third parameter is the encoding format//relative address, generally loading local file is not used, you can refer to the        Find the relevant file in the BaseURL. Load the files in the sandbox as binary data,//NSData *data = [NSData datawithcontentsoffile:path];////[Self.webview loaddata:data MIME type:@ "Application/vnd.openxmlformats-officedocument.wordprocessingml.document" textEncodingName:@ "UTF-8" Baseurl:nil];}



UIWebView three ways to load content:

1 loading Local data files

Specify the mimetype of the file

Encoding format using @ "UTF-8"

2 Loading HTML strings (all or part of the HTML file can be loaded)


3 Loading the Nsurlrequest file (the first two steps are the same as Nsurlconnect)




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.