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 loading local files, you can use the way you load data
//The first argument is a nsdata, the data corresponding to the local file
//The second parameter is MimeType
///The third parameter is the encoding format
//relative address, general load local files are not used, you can find related files in the specified baseurl.

//load 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:nil];
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 loads a 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 a 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:nil];
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 = [Nsurlrequest Requestwithurl:url];
2NSURLConnection

3 in Nsurlresponse, the server tells the browser how to open the file.

Go to MimeType after using the Sync method
Nsurlresponse *response = nil;
[Nsurlconnection sendsynchronousrequest:request returningresponse:&response Error:nil];
return response. MIMEType;
}

UIWebView three ways to load local data

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.