IOS Webview JS Interactive event interception acquisition

Source: Internet
Author: User

The recent project has used the Webvvew,webview loaded page has two buttons, in the app to respond to the two button click events, and accept the JS pass over the parameters.

iOS7, came out a new class library called Javascriptcore.framework

-(void) setupjscontent{//Get current JS environment _content = [_webview valueforkeypath:@ "DocumentView.webView.mainFrame.javaScriptC    Ontext "]; Print exception _content.exceptionhandler = ^ (Jscontext *context, Jsvalue *exceptionvalue) {context.exception = E        Xceptionvalue;    Loginfo (@ "%@", Exceptionvalue);    };    Gets the JS event WS (bself);        _content[@ "Showtag"] = ^ (int num,nsstring *tag) {loginfo (@ "num =%d \ n Tag =%@", Num,tag); if (num = = 1) {//view the location of the project//get latitude and longitude nsarray *locationarray = [Tag componentsseparatedbystring:@ ","            ];            Projectlocationviewcontroller *locationctrl = [[Projectlocationviewcontroller alloc]init];            Locationctrl.longitude = [locationarray[0] floatvalue];            Locationctrl.latitude = [locationarray[1] floatvalue];        [Bself.navigationcontroller Pushviewcontroller:locationctrl Animated:yes]; }else if (num = = 2) {Uialertview *alter = [[Uialertview alloc]initwithtitle:@ ' title ' Message:@ "Content" Delegate:nil cancelbuttontitle:@ "Cancel" otherbuttontitles:@ "OK", nil];                        Alter.alertviewstyle = Uialertviewstyleplaintextinput;        [Alter show]; }    };}

Block JS "Showtag" event, and accept parameters, custom response events
IOS WebView js Interaction There is also a third-party library address: Https://github.com/marcuswestin/WebViewJavascriptBridge

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

IOS Webview JS Interactive event interception acquisition

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.