Three ways to load UIWebView

Source: Internet
Author: User

First, use UIWebView to embed Web content on your app.

The API provides three ways to:

    1. -(void) Loadrequest: (nsurlrequest *) request;
    2. -(void) loadhtmlstring: (NSString *) string BaseURL: (Nsurl *) BaseURL;
    3. -(void) LoadData: (NSData *) data MIMEType: (NSString *)
    4. MIMEType textencodingname: (NSString *) textencodingname BaseURL: (Nsurl *) BaseURL;

1, give the URL address directly to load the Web content.

    1. NSString *Path = @ "http://theo2life.com";
    2. Nsurl *URL = [[Nsurl alloc] initwithstring:path];
    3. [Self.webview loadrequest:[nsurlrequest Requestwithurl:url];

2. Embed local HTML file contents into WebView

    1. NSString *ResourcePath = [[NSBundle mainbundle] resourcepath];
    2. NSString *FilePath = [ResourcePath stringbyappendingpathcomponent:@ "test.html"];
    3. NSString *htmlstring =[[nsstring alloc] Initwithcontentsoffile:filepath encoding:nsutf8stringencoding Error : nil];
    4. [Self.webview loadhtmlstring:htmlstring baseurl:[nsurl fileurlwithpath: [[NSBundle Mainbundle] bundlePath]];

You can also do this if you do not load from an HTML file:

    1. NSString *HTMLData = @ " src=\" test2.png\ " />ddd";
    2. [Self.webview loadhtmlstring:htmldata baseurl:[nsurl fileurlwithpath: [[NSBundle Mainbundle] bundlePath]];
    3. Baseurl:[nsurl Fileurlwithpath: [[NSBundle Mainbundle] Bundlepath]

This paragraph indicates the basic path of other file resources referenced by HTMLData, if the Baseurl:nil picture information will not be displayed ~

3, the same 2, in more detail gives the Web content encoding method.

Second, other operations:

If the loaded Web layout is larger than the size of the ipad, the part that is found to be out is blank, set webview.scalespagetofit = YES, and make the Web content layout adaptable to WebView.

Three ways to load UIWebView

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.