IOS WebView The answer to the question you need

Source: Internet
Author: User

UIWebView you can load and display a Web page for a URL, or you can display a local HTML-based page or some Web page:

A. Load URL

  1. WebView = [[UIWebView alloc] Initwithframe:cgrectmake (0, 44, 320, 400)];
  2. nsstring *path = @"http://www.baidu.com";
  3. Nsurl *url = [Nsurl Urlwithstring:path];
  4. [WebView loadrequest:[nsurlrequest Requestwithurl:url];

B. Loading HTML

nsbundle *bundle = [NSBundle mainbundle];
NSString *respath = [bundle ResourcePath];
NSString *filepath = [respathstringbyappendingpathcomponent:@ "home.html"];
[WebView Loadhtmlstring:[nsstringstringwithcontentsoffile:filepath]
Baseurl:[nsurlfileurlwithpath:[bundle Bundlepath]];

Second, the use of Web page loading instructions, loading completed and then display the page out

The first thing to specify is the delegate method:

WebView. delegate = self;

UIWebView has the following principal methods:

1,-(void) Webviewdidstartload: (UIWebView *) WebView; The method is executed when the load is started.
2,-(void) Webviewdidfinishload: (UIWebView *) WebView; The method is executed when the load is complete.
3,-(void) WebView: (UIWebView *) WebView didfailloadwitherror: (nserror*) error; The method is executed when the load error occurs.

This allows you to implement this functionality using the Webviewdidstartload and Webviewdidfinishload methods:

  1. -(void) Webviewdidstartload: (UIWebView *) WebView
  2. {
  3. //Create Uiactivityindicatorview back-bottom translucent view
  4. UIView *view = [[UIView alloc] Initwithframe:cgrectmake (0, 0, 320, 480)];
  5. [View settag:108];
  6. [View Setbackgroundcolor:[uicolor Blackcolor];
  7. [View setalpha:0.5];
  8. [Self.view Addsubview:view];
  9. Activityindicator = [[Uiactivityindicatorview alloc] Initwithframe:cgrectmake (0.0f, 0.0f, 32.0f, 32.0f)];
  10. [Activityindicator SetCenter:view.center];
  11. [Activityindicator Setactivityindicatorviewstyle:uiactivityindicatorviewstylewhite];
  12. [View Addsubview:activityindicator];
  13. [Activityindicator startanimating];
  14. }
  15. -(void) Webviewdidfinishload: (UIWebView *) WebView
  16. {
  17. [Activityindicator stopanimating];
  18. UIView *view = (uiview*) [Self.view viewwithtag:108];
  19. [View Removefromsuperview];
  20. NSLog (@"webviewdidfinishload");
  21. }

UIWebView can also execute JavaScript code through the Stringbyevaluatingjavascriptfromstring function, allowing for more flexibility in working with Web page files

1. Get the title of UIWebView
NSString *thetitle=[webviewstringbyevaluatingjavascriptfromstring:@ "Document.title"];

2. Get the URL of the page

Nsstring*currenturl = [webviewstringbyevaluatingjavascriptfromstring:@ "Document.location.href"];

3. Modifying the value of a page element

Nsstring*js_result = [webviewstringbyevaluatingjavascriptfromstring:@ "Document.getelementsbyname (' Q ') [0].value=] Zhu Qilin '; "];

4. Form submission

NSSTRING*JS_RESULT2 = [webviewstringbyevaluatingjavascriptfromstring:@ "Document.forms[0].submit ();"]

Four, the comprehensive use of the above methods, but also to achieve the display of some Web pages, the basic ideas are as follows:

1. Use Loadrequest to get the webpage first, but set uiwebview.hidden = YES first;

2. In Webviewdidfinishload, use

NSSTRING*JS_RESULT2 = [webviewstringbyevaluatingjavascriptfromstring:@ "document.getelementbyidx_x_x_xx_x_x (' xxxx '). InnerHTML; "] Gets the HTML code for the specified label element3. Then use loadhtmlstring to load the HTML code and

IOS WebView The answer to the question you need

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.