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