iOS elearning------4 UIWebView Three ways to load local dataCategory: IOS 2014-06-27 12:56 7078 People read comments (0) favorite reports
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):
[OBJC]View Plaincopy
- -(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.
- //Use the Sync method to 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):
[OBJC]View Plaincopy
- -(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
- 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 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";
- }
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)
iOS elearning------4 UIWebView Three ways to load local data