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
- WebView = [[UIWebView alloc] Initwithframe:cgrectmake (0, 44, 320, 400)];
- nsstring *path = @"http://www.baidu.com";
- Nsurl *url = [Nsurl Urlwithstring:path];
- [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:
- -(void) Webviewdidstartload: (UIWebView *) WebView
- {
- //Create Uiactivityindicatorview back-bottom translucent view
- UIView *view = [[UIView alloc] Initwithframe:cgrectmake (0, 0, 320, 480)];
- [View settag:108];
- [View Setbackgroundcolor:[uicolor Blackcolor];
- [View setalpha:0.5];
- [Self.view Addsubview:view];
- Activityindicator = [[Uiactivityindicatorview alloc] Initwithframe:cgrectmake (0.0f, 0.0f, 32.0f, 32.0f)];
- [Activityindicator SetCenter:view.center];
- [Activityindicator Setactivityindicatorviewstyle:uiactivityindicatorviewstylewhite];
- [View Addsubview:activityindicator];
- [Activityindicator startanimating];
- }
- -(void) Webviewdidfinishload: (UIWebView *) WebView
- {
- [Activityindicator stopanimating];
- UIView *view = (uiview*) [Self.view viewwithtag:108];
- [View Removefromsuperview];
- NSLog (@"webviewdidfinishload");
- }
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