Now, a good iOS app is sometimes associated with web content, and of course it also adds to the user experience, and it's better and more effective at showing the app's functionality.
Because I will use this part of the project in the function, make the record!
JS Call OC
Many applications inside more or less call the Web page, to achieve gorgeous effect, the so-called JS call oc ... For instance, there's a button on the Web page.
Click the button to jump the interface, the jump action by the OC Code implementation.
OC Call JS
Or for example, our OC code creates an input box, such as entering a user name, which is displayed on the Web page when it is finished, displaying the user's user name
I. Using WebView proxy method to realize the mutual invocation between OC and JS
Creating properties
@property (Nonatomic,strong) UIWebView * webview;
Comply with the WebView agreement uiwebviewdelegate
-(void) webviewload{
nsurl * url = [[Nsurl alloc]initwithstring:@ ' http://www.baidu.com '];
Self.webview = [[UIWebView alloc]initwithframe:self.view.bounds];
Nsurlrequest * request = [[Nsurlrequest alloc]initwithurl:url];
Self.webView.delegate = self;
[Self.webview loadrequest:request];
[Self.view AddSubview:self.webView];
}
JS Invoke OC
-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype :(uiwebviewnavigationtype) navigationtype{
nsstring * str = Request. url.relativestring;
if ([str isequaltostring:@ "http://www.baidu.com/"]) {
NSLog (@ "to find Niang");
}
Return YES
}
OC calls JS
-(void) Webviewdidfinishload: (UIWebView *) webview{
nsstring * str = [Self.webview stringbyevaluatingjavascriptfromstring:@ "document.getElementById" ("" "index-kw" "\"). Value= "" "Li" "\" "];
NSLog (@ "webviewdidfinishload=%@", str);
}
Two. Implementation using a third party framework (the name of the person who needs you and the HTML side to unify the method)
-(void) webviewbridge{Self.webview = [[UIWebView alloc]initwithframe:[uiscreen mainscreen].bounds];
NSString * Path = [[NSBundle mainbundle]pathforresource:@ ' exampleapp.html ' oftype:nil];
Nsurl * url = [[Nsurl Alloc]initfileurlwithpath:path];
[Self.webview loadrequest:[nsurlrequest Requestwithurl:url]];
Self.webView.delegate = self;
[Self.view AddSubview:self.webView];
Setting can bridge [Webviewjavascriptbridge enablelogging];
Set up bridging Self.bridge = [Webviewjavascriptbridge BridgeForWebView:self.webView];
Set agent [Self.bridge setwebviewdelegate:self]; JS Call OC (Testobjccallback is the same as the HTML uniform method name) [Self.bridge registerhandler:@ "Testobjccallback" handler:^ (ID data,
Wvjbresponsecallback responsecallback) {NSLog (@ "button clicked");
self.view.backgroundcolor= [Uicolor Blackcolor];
Responsecallback (@ "button clicked");
}];
//oc calls JS-(void) Webviewdidfinishload: (UIWebView *) webview{NSLog (@ "Webviewdidfinishload");
[Self.bridge callhandler:@ "Registerhandler"];
}
Thank you for reading, I hope to help you, thank you for your support for this site!