IOS, ios9
UIWebView is a built-in IOS browser that allows you to browse webpages and open documents in html/htm pdf docx txt and other formats. Safari is implemented through UIWebView.
The server puts MIME identifiers in the transmitted data and tells the browser to use the plug-in to read the relevant files.
Uiwebview loads various local files (using 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: loads local files. You can use the data loading method.
// The first parameter is NSData, which corresponds to the local file.
// The second parameter is MIMEType.
// The third parameter is the encoding format.
// Relative address. Generally, it is not used to load local files. You can search for related files in the specified baseURL.
// Load files in the sandbox in the form of binary data,
NSData * data = [NSData dataWithContentsOfFile: path];
[Self. webView loadData: data MIMEType: @ "application/vnd.openxmlformats-officedocument.wordprocessingml.doc ument" textEncodingName: @ "UTF-8" baseURL: nil];
}
# Pragma mark load docx files
-(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.doc ument" textEncodingName: @ "UTF-8" baseURL: nil];}
# Pragma mark loading PDF files
-(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 load local text files
-(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: load a 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
{
// 1 NSURLRequest
NSURLRequest * request = [NSURLRequest requestWithURL: url];
// 2 NSURLConnection
// 3 in NSURLResponse, the server tells the browser how to open the file.
// Use the synchronization method and go to MIMEType
NSURLResponse * response = nil;
[NSURLConnection sendSynchronousRequest: request returningResponse: & response error: nil];
Return response. MIMEType;
}
-(Void) viewDidLoad
{
[Super viewDidLoad];
[Self setupUI];
// The first method of loading the path
// NSString * path = [[NSBundle mainBundle] pathForResource: @ "about .docx" ofType: nil];
// NSURL * url = [NSURL fileURLWithPath: path];
// The second method of loading the path
NSURL * url = [[NSBundle mainBundle] URLForResource: @ "ios6cookbooksion" withExtension: nil];
// The second method for loading files in uiwebview. The first method is in the annotations below.
NSURLRequest * request = [NSURLRequest requestWithURL: url];
[Self. webView loadRequest: request];
// NSLog (@ "% @", [self mimeType: url]);
// Webview: loads local files. You can use the data loading method.
// The first parameter is NSData, which corresponds to the local file.
// The second parameter is MIMEType.
// The third parameter is the encoding format.
// Relative address. Generally, it is not used to load local files. You can search for related files in the specified baseURL.
// Load files in the sandbox in the form of binary data,
// NSData * data = [NSData dataWithContentsOfFile: path];
//
// [Self. webView loadData: data MIMEType: @ "application/vnd.openxmlformats-officedocument.wordprocessingml.doc ument" textEncodingName: @ "UTF-8" baseURL: nil];
}
There are three ways for UIWebView to load content:
1. Load local data files
MIMEType of the specified file
Encoding format using @ "UTF-8"
2. load html strings (all or part of html files can be loaded)
3. Load the NSURLRequest file (the first two steps are the same as NSURLConnect)