IOS Pure Code Adaptation iphone6,6+

Source: Internet
Author: User

Link Address: http://blog.csdn.net/codywangziham01/article/details/37658399

Transferred from: http://www.maxiaoguo.com/clothes/255.html

UIWebView is a box that is used to load data from a Web page. UIWebView can be used to load PDF Word doc and so on file

There are two ways to generate WebView, 1, storyboard by dragging 2, initializing by Alloc init.

Create WebView, _webview.datadetectortypes = Uidatadetectortypeall in the following text; is to identify the type in the webview, such as when there is a phone number in WebView, click on the number to call directly

[OBJC]View PlainCopy
    1. -(UIWebView *) WebView
    2. {
    3. if (!_webview) {
    4. _webview = [[UIWebView alloc] initwithframe:self. View. bounds];
    5. _webview. datadetectortypes = Uidatadetectortypeall;
    6. }
    7. return _webview;
    8. }

Loading Web pages

[OBJC]View PlainCopy
  1. Let the browser load the specified string and search using m.baidu.com
  2. -(void) loadString: (nsstring *) str
  3. {
  4. //1. URL location resource, address of resource required
  5. nsstring *urlstr = str;
  6. if (![ STR hasprefix:@ "/http"]) {
  7. URLSTR = [NSString stringwithformat:@ "http://m.baidu.com/s?word=%@", str];
  8. }
  9. Nsurl *url = [Nsurl urlwithstring:urlstr];
  10. //2. Tell the URL to the server, request, request data from m.baidu.com
  11. nsurlrequest *request = [nsurlrequest requestwithurl:url];
  12. //3. Send the request to the server
  13. [self. WebView Loadrequest:request];
  14. }

Loading HTML

[OBJC]View PlainCopy
    1. HTML is the design language of a Web page
    2. //<> indicates tags </>
    3. //Application scenario: Capturing a portion of a Web page display
    4. //For example: the full content of the Web page contains ads! After loading the completion page, remove the HTML from the ad section and then load
    5. //used by many news applications
    6. [self. WebView loadhtmlstring:@ "<p>Hello</p>" BaseURL: nil];


Loading local files

[OBJC]View PlainCopy
  1. #pragma mark-load file
  2. -(void) LoadFile
  3. {
  4. //Application scenario: Load files downloaded from the server, such as PDF, or word, picture, etc.
  5. Nsurl *fileurl = [[NSBundle Mainbundle] urlforresource:@ "about. txt" withextension: nil];
  6. nsurlrequest *request = [nsurlrequest requestwithurl:fileurl];
  7. [self. WebView Loadrequest:request];
  8. }

Load local files in a two-tier manner

[OBJC]View PlainCopy
    1. #pragma loading files as binary data
    2. -(void) Loaddatafile
    3. {
    4. //One of the most common cases
    5. //Open IE, visit website, prompt you to install Flash plugin
    6. //If you do not have this application, it is not possible to open the corresponding file with UIWebView
    7. //Application scenario: Load files downloaded from the server, such as PDF, or word, picture, etc.
    8. Nsurl *fileurl = [[NSBundle Mainbundle] urlforresource:@ "IOS 7 programming Cookbook.pdf"   Withextension: nil];
    9. nsurlrequest *request = [nsurlrequest requestwithurl:fileurl];
    10. //Server response object, the server receives the request returned to the client
    11. nsurlresponse *respnose = nil;
    12. NSData *data = [nsurlconnection sendsynchronousrequest:request returningresponse:&respnose Error  :NULL];
    13. NSLog (@ "%@", Respnose.  MIMEType);
    14. //In IOS development, if not a special requirement, all text encodings are UTF8
    15. //Use UTF8 to interpret the received binary data stream first
    16. [self. WebView loaddata:data mimetype:respnose.  MIMEType textencodingname:@ "UTF8" BaseURL: nil];
    17. }

IOS Pure Code Adaptation iphone6,6+

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.