With JS interactive oc call JS
// 执行 js- (void)webViewDidFinishLoad:(UIWebView *)webView { NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title;"]; NSLog(@"%@", title); [webView stringByEvaluatingJavaScriptFromString:@"clickme();"];}
JS Call OC Prepare code
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSLog(@"%@", request.URL); return YES;}
In OC, if the proxy method returns a BOOL type, return YES will execute normally
Interpreting custom Protocols
href="myfunc:///showMessage:/周末一起玩吧:D"调用 OC 中的方法 `showMessage:` 显示内容 `郊游怎么样:D`
Code implementation
- (BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (Nsurlrequest *) Request Navigationtype: (Uiwebviewnavigationtype) Navigationtype {NSLog (@ "%@", request. Url. Scheme); if ([Request]. URL. Scheme isequaltostring:@ "MyFunc"]) { NSLog (@ "%@", request. URL. pathcomponents); Nsarray *components = Request. URL. pathcomponents; nsstring *funcname = components[1]; nsstring *param = components[2]; SEL func = nsselectorfromstring (funcName); [Selfperformselector:func withobject:param];} return YES;}
Code details
- (BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (Nsurlrequest *) Request Navigationtype: (Uiwebviewnavigationtype) Navigationtype {NSLog (@ "%@", request. Url. Scheme);if ([Request. Url. Scheme isequaltostring:@ "MyFunc"]) {NSLog (@ "%@", request. Url. pathcomponents);Nsarray *components = Request. Url. pathcomponents;if (components. Count! =3) {return NO;} nsstring *funcname = Components[1]; nsstring *param = Components[2]; SEL func = nsselectorfromstring (funcName); if ([self Respondstoselector:func]) { #pragma clang diagnostic push #pragma clang diagnostic ignored Span class= "Hljs-title" > "-warc-performselector-leaks" [self Performselector:func Withobject:param]; #pragma clang diagnostic pop} return NO;} return yes;}
WebView and JS interaction