webView直接加载html显示,当点击html上js响应时,如果要想在webView中执行操作,则需要实现以下方法
/**
* 通过这个方法完成JS调用OC
* JS和OC交互的第三方框架:WebViewJavaScriptBridge
*/
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
// url == xmg://sendMessage_?200
NSString *url = request.URL.absoluteString;
NSString *scheme = @"xmg://";
if ([url hasPrefix:scheme]) {
// 获得协议后面的路径 path == sendMessage_number2_?200&300
NSString *path = [url substringFromIndex:scheme.length];
// 利用?切割路径
NSArray *subpaths = [path componentsSeparatedByString:@"?"];
// 方法名 methodName == sendMessage:number2:
NSString *methodName = [[subpaths firstObject] stringByReplacingOccurrencesOfString:@"_" withString:@":"];
// 参数 200&300
NSString *param = [subpaths lastObject];
NSArray *subparams = nil;
if (subpaths.count == 2 || [param containsString:@"&"]) {
subparams = [param componentsSeparatedByString:@"&"];
}
// 取出前面的2个参数
NSString *firstParam = [subparams firstObject];
NSString *secondParam = subparams.count <= 1 ? nil : [subparams lastObject];
//在ios中执行相关代码
// [self performSelector:NSSelectorFromString(methodName) withObject:firstParam withObject:secondParam];
return NO;
}
NSLog(@"想加载其他请求,不是想调用OC的方法");
return YES;
}