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

Source: Internet
Author: User
Tags vars

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
  1. -(void) Viewdidload
  2. {
  3. [Super Viewdidload];
  4. [self setupui];
  5. nsstring *path = [[NSBundle Mainbundle] pathforresource:@ "about. docx" ofType: nil];
  6. Nsurl *url = [Nsurl Fileurlwithpath:path];
  7. NSLog (@ "%@", [self Mimetype:url]);
  8. //webview loading local files, you can use the way you load data
  9. //The first argument is a nsdata, the data corresponding to the local file
  10. //The second parameter is MimeType
  11. ///The third parameter is the encoding format
  12. //relative address, general load local files are not used, you can find related files in the specified baseurl.
  13. //load the files in the sandbox as binary data,
  14. NSData *data = [NSData Datawithcontentsoffile:path];
  15. [self. WebView loaddata:data MIMEType:@ "application/ Vnd.openxmlformats-officedocument.wordprocessingml.document " textencodingname:@" UTF-8 " BaseURL:  NIL];
  16. }
  17. #pragma mark load docx file
  18. -(void) Loaddocx
  19. {
  20. nsstring *path = [[NSBundle Mainbundle] pathforresource:@ "about. docx" ofType: nil];
  21. Nsurl *url = [Nsurl Fileurlwithpath:path];
  22. NSLog (@ "%@", [self Mimetype:url]);
  23. NSData *data = [NSData Datawithcontentsoffile:path];
  24. [self. WebView loaddata:data MIMEType:@ "application/ Vnd.openxmlformats-officedocument.wordprocessingml.document " textencodingname:@" UTF-8 " BaseURL:  NIL];}
  25. #pragma mark loads a PDF file
  26. -(void) loadpdf
  27. {
  28. nsstring *path = [[NSBundle Mainbundle] pathforresource:@ "ios6cookbook.pdf" ofType: nil];
  29. Nsurl *url = [Nsurl Fileurlwithpath:path];
  30. NSLog (@ "%@", [self Mimetype:url]);
  31. NSData *data = [NSData Datawithcontentsoffile:path];
  32. [self. WebView loaddata:data MIMEType:@ "Application/pdf" textencodingname:@ "UTF-8"   BaseURL: nil];
  33. }
  34. #pragma mark loads a local text file
  35. -(void) LoadText
  36. {
  37. nsstring *path = [[NSBundle Mainbundle] pathforresource:@ "about. txt" ofType: nil];
  38. Nsurl *url = [Nsurl Fileurlwithpath:path];
  39. NSLog (@ "%@", [self Mimetype:url]);
  40. NSData *data = [NSData Datawithcontentsoffile:path];
  41. [self. WebView loaddata:data MIMEType:@ "Text/plain" textencodingname:@ "UTF-8"   BaseURL: nil];
  42. }
  43. #pragma mark loads the local HTML file
  44. -(void) loadhtml
  45. {
  46. nsstring *path = [[NSBundle Mainbundle] pathforresource:@ "demo.html" ofType: nil];
  47. Nsurl *url = [Nsurl Fileurlwithpath:path];
  48. NSLog (@ "%@", [self Mimetype:url]);
  49. NSData *data = [NSData Datawithcontentsoffile:path];
  50. [self. WebView loaddata:data MIMEType:@ "text/html" textencodingname:@ "UTF-8"   BaseURL: nil];
  51. }
  52. #pragma mark gets the mimetype type of the specified URL
  53. -(NSString *) MimeType: (nsurl *) URL
  54. {
  55. //1nsurlrequest
  56. nsurlrequest *request = [nsurlrequest requestwithurl:url];
  57. //2nsurlconnection
  58. //3 in Nsurlresponse, the server tells the browser how to open the file.
  59. //Use the Sync method to go to MimeType
  60. nsurlresponse *response = nil;
  61. [Nsurlconnection sendsynchronousrequest:request returningresponse:&response error: nil];
  62. return response.  MIMEType;
  63. }



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

[OBJC]View Plaincopy
  1. -(void) Viewdidload
  2. {
  3. [Super Viewdidload];
  4. [self setupui];
  5. //The first way to load a path
  6. NSString *path = [[NSBundle mainbundle] pathforresource:@ "about. docx" Oftype:nil];
  7. Nsurl *url = [Nsurl Fileurlwithpath:path];
  8. //The second way to load a path
  9. Nsurl *url = [[NSBundle Mainbundle] urlforresource:@ "ios6cookbook.pdf" withextension: nil];
  10. //uiwebview The second way to load a file.  The first method is in the comments below.
  11. nsurlrequest *request = [nsurlrequest requestwithurl:url];
  12. [self. WebView Loadrequest:request];
  13. //nslog (@ "%@", [self mimetype:url]);
  14. //webview loading local files, you can use the way you load data
  15. //The first argument is a nsdata, the data corresponding to the local file
  16. //The second parameter is MimeType
  17. ///The third parameter is the encoding format
  18. //relative address, general load local files are not used, you can find related files in the specified baseurl.
  19. //load the files in the sandbox as binary data,
  20. NSData *data = [NSData Datawithcontentsoffile:path];
  21. //
  22. [Self.webview loaddata:data mimetype:@ "application/vnd.openxmlformats-officedocument.wordprocessingml.document "textencodingname:@" UTF-8 "Baseurl:nil";
  23. }



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)

http://blog.csdn.net/huang2009303513/article/details/35210425

iOS elearning------4 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.