1 Basic usage
1. Instantiation: UIWebView *webview = [[UIWebView alloc] initWithFrame:self.view.window.bounds];
2, configuration parameters: Delegate, Scalespagetofit, etc.
3. Load request: [WebView loadrequest:nsrequest instance];
4, the delegation method:
1), Complete loading
-(void) Webviewdidfinishload: (UIWebView *) WebView
2 manipulating JavaScript
1. Get the web address
NSString *currenturl = [WebView stringbyevaluatingjavascriptfromstring:@ "Document.location.href"];
2. Get the page title
NSString *title = [WebView stringbyevaluatingjavascriptfromstring:@ "Document.title"];
3. Modify Font Size
NSString *jsstring = [[NSString alloc] initwithformat:@ "document.getelementsbytagname (' body ') [0]. style.webkittextsizeadjust= '%d%% ' ",
50];
[WebView stringbyevaluatingjavascriptfromstring:jsstring];
4, Horizontal Vertical Center
NSString *bodystylevertical = @ "document.getElementsByTagName (' body ') [0].style.verticalalign = ' middle ';";
NSString *bodystylehorizontal = @ "document.getElementsByTagName (' body ') [0].style.textalign = ' center ';";
NSString *mapstyle = @ "document.getElementById (' MapID ')." Style.margin = ' auto '; ";
[WebView stringbyevaluatingjavascriptfromstring:bodystylevertical];
[WebView Stringbyevaluatingjavascriptfromstring:bodystylehorizontal];
[WebView Stringbyevaluatingjavascriptfromstring:mapstyle];
5. Modify the font color
[WebView stringbyevaluatingjavascriptfromstring:@ "document.getelementsbytagname (' body ') [0]. Style.webkittextfillcolor= ' Red ' "];
6. Modify the page background color
[WebView stringbyevaluatingjavascriptfromstring:@ "document.getelementsbytagname (' body ') [0].style.background= ' Black ' "];
7. Change the image size
1 [WebView stringbyevaluatingjavascriptfromstring:2 @"var script = document.createelement (' script ');"3 "script.type = ' text/javascript ';"4 "script.text = \ "Function resizeimages () {"5 "var myimg,oldwidth;"6 "var maxwidth=320;" //Zoom Factor7 "For (I=0;i <document.images.length;i++) {"8 "myimg = document.images[i];"9 "if (Myimg.width > MaxWidth) {"Ten "oldwidth = myimg.width;" One "myimg.width = maxwidth;" A "myimg.height = myimg.height * (maxwidth/oldwidth);" - "}" - "}" the "}\";" - "document.getelementsbytagname (' head ') [0].appendchild (script);"]; - -[WebView stringbyevaluatingjavascriptfromstring:@"resizeimages ();"];change the size of a picture
8. Submit a Form
1NSString * Currenturl = [WebView stringbyevaluatingjavascriptfromstring:@"Document.location.href"];2 //Get title3NSString * title = [WebView stringbyevaluatingjavascriptfromstring:@"Document.title"];4 //set up a form5NSString * Js_result = [WebView stringbyevaluatingjavascriptfromstring:@"document.getelementsbyname (' Q ') [0].value= ' username ';"];6 //Submit Form7NSString * JS_RESULT2 = [WebView stringbyevaluatingjavascriptfromstring:@"document.forms[0].submit ();"];8 9NSLog (@"pp%@,qq%@,ww%@,ee%@", CURRENTURL,TITLE,JS_RESULT,JS_RESULT2);Submit Form
9. View Source Code
NSString *jstogethtmlsource = @ "document.getelementsbytagname (' html ') [0].innerhtml];
NSString *htmlsource = [Webview_ Stringbyevaluatingjavascriptfromstring:jstogethtmlsource];
NSLog (@ "%@", Htmlsource);
10. Long Press Menu
[WebView stringbyevaluatingjavascriptfromstring:@ "document.documentelement.style.webkituserselect= ' none ';"];
11. Scroll to a location
[WebView stringbyevaluatingjavascriptfromstring:@ "Window.scrollto (0,200);"];
12. Get Picture gestures
UITapGestureRecognizer *doubletap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (Doubletap:) ];
doubletap.numberoftouchesrequired = 2;
[WebView Addgesturerecognizer:doubletap];
13. Get the image address
1- (void) Doubletap: (uitapgesturerecognizer*) Sender {2 //<find HTML tag which was clicked by User>3 //<if tag is IMG, then get image URL and start saving>4UIWebView *webview = (uiwebview*) [Self.window Viewwithtag: +];5 intScrollpositiony = [[WebView stringbyevaluatingjavascriptfromstring:@"Window.pageyoffset"] intvalue];6 intScrollpositionx = [[WebView stringbyevaluatingjavascriptfromstring:@"Window.pagexoffset"] intvalue];7 8 intDisplaywidth = [[WebView stringbyevaluatingjavascriptfromstring:@"Window.outerwidth"] intvalue];9CGFloat scale = webView.frame.size.width/Displaywidth;Ten OneCgpoint pt =[Sender Locationinview:webview]; APt.x *=Scale ; -Pt.y *=Scale ; -Pt.x + =Scrollpositionx; thePt.y + =Scrollpositiony; - -NSString *js = [NSString stringWithFormat:@"Document.elementfrompoint (%f,%f). TagName", Pt.x, Pt.y]; -NSString * TagName =[WebView Stringbyevaluatingjavascriptfromstring:js]; + if([TagName isequaltostring:@ "IMG"]) {//img may change -NSString *imgurl = [NSString stringWithFormat:@"Document.elementfrompoint (%f,%f). SRC", Pt.x, Pt.y]; +NSString *urltosave =[WebView Stringbyevaluatingjavascriptfromstring:imgurl]; ANSLog (@"Image url=%@", urltosave); at } - - -}Get Image Address
14. Get the video address
1NSString *LJS2 =@"(document.getElementsByTagName (\"Video\") [0]). getElementsByTagName (\ "source\") [0].src";//Qiyi2NSString *LJS2 =@"(document.getElementsByTagName (\"Video\") [0]). SRC";//Youku,tudou,ku6, Souhu3NSString *lm3u8 =[WebView stringbyevaluatingjavascriptfromstring:ljs2];4NSLog (@"Video source:%@", lm3u8);
Easy use of IOS 8:uiwebview