iOS与js交互的2种方法
2017-03-24 12:02
246 查看
- (void)viewDidLoad {
[superviewDidLoad];
UIWebView *webView = [[UIWebViewalloc] initWithFrame:self.view.bounds];
NSString *filePath = [[NSBundlemainBundle] pathForResource:@"jsOC1"ofType:@"html"];
[webView loadRequest:[NSURLRequestrequestWithURL:[NSURLfileURLWithPath:filePath]]];
webView.delegate =self;
[self.viewaddSubview:webView];
}
//1、获取当前页面的url,。
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *currentURL = [webViewstringByEvaluatingJavaScriptFromString:@"document.location.href"];
NSString *title = [webViewstringByEvaluatingJavaScriptFromString:@"document.title"];
JSContext *jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
JSValue *jsValue = [jsContextevaluateScript:@"showAppAlertMsg"];
[jsValue callWithArguments:@[@"这是app本地交互文案"]];
NSLog(@"currentURL ===%@\ntitle ===%@\njsValue ===%@",currentURL,title,jsValue);
//定义好JS要调用的方法, share就是调用的share方法名
jsContext[@"secondClick"] = ^() {
NSLog(@"+++++++Begin Log+++++++");
NSArray *args = [JSContextcurrentArguments];
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alertView = [[UIAlertViewalloc] initWithTitle:@"方式二"message:@"这是OC原生的弹出窗"delegate:selfcancelButtonTitle:@"收到"otherButtonTitles:nil];
[alertView show];
});
for (JSValue *jsValin args) {
NSLog(@"%@", jsVal.toString);
}
NSLog(@"-------End Log-------");
};
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL * url = [requestURL];
NSLog(@"urlScheme:%@",[urlscheme]);
if ([[urlscheme] isEqualToString:@"firstclick"]) {
NSArray *params =[url.querycomponentsSeparatedByString:@"&"];
NSMutableDictionary *tempDic = [NSMutableDictionarydictionary];
for (NSString *paramStrin params) {
NSArray *dicArray = [paramStrcomponentsSeparatedByString:@"="];
if (dicArray.count >1) {
NSString *decodeValue = [dicArray[1]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[tempDic setObject:decodeValueforKey:dicArray[0]];
}
}
UIAlertView *alertView = [[UIAlertViewalloc] initWithTitle:@"方式一"message:@"这是OC原生的弹出窗"delegate:selfcancelButtonTitle:@"收到"otherButtonTitles:nil];
[alertView show];
NSLog(@"tempDic:%@",tempDic);
returnNO;
}
returnYES;
}
[superviewDidLoad];
UIWebView *webView = [[UIWebViewalloc] initWithFrame:self.view.bounds];
NSString *filePath = [[NSBundlemainBundle] pathForResource:@"jsOC1"ofType:@"html"];
[webView loadRequest:[NSURLRequestrequestWithURL:[NSURLfileURLWithPath:filePath]]];
webView.delegate =self;
[self.viewaddSubview:webView];
}
//1、获取当前页面的url,。
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *currentURL = [webViewstringByEvaluatingJavaScriptFromString:@"document.location.href"];
NSString *title = [webViewstringByEvaluatingJavaScriptFromString:@"document.title"];
JSContext *jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
JSValue *jsValue = [jsContextevaluateScript:@"showAppAlertMsg"];
[jsValue callWithArguments:@[@"这是app本地交互文案"]];
NSLog(@"currentURL ===%@\ntitle ===%@\njsValue ===%@",currentURL,title,jsValue);
//定义好JS要调用的方法, share就是调用的share方法名
jsContext[@"secondClick"] = ^() {
NSLog(@"+++++++Begin Log+++++++");
NSArray *args = [JSContextcurrentArguments];
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alertView = [[UIAlertViewalloc] initWithTitle:@"方式二"message:@"这是OC原生的弹出窗"delegate:selfcancelButtonTitle:@"收到"otherButtonTitles:nil];
[alertView show];
});
for (JSValue *jsValin args) {
NSLog(@"%@", jsVal.toString);
}
NSLog(@"-------End Log-------");
};
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL * url = [requestURL];
NSLog(@"urlScheme:%@",[urlscheme]);
if ([[urlscheme] isEqualToString:@"firstclick"]) {
NSArray *params =[url.querycomponentsSeparatedByString:@"&"];
NSMutableDictionary *tempDic = [NSMutableDictionarydictionary];
for (NSString *paramStrin params) {
NSArray *dicArray = [paramStrcomponentsSeparatedByString:@"="];
if (dicArray.count >1) {
NSString *decodeValue = [dicArray[1]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[tempDic setObject:decodeValueforKey:dicArray[0]];
}
}
UIAlertView *alertView = [[UIAlertViewalloc] initWithTitle:@"方式一"message:@"这是OC原生的弹出窗"delegate:selfcancelButtonTitle:@"收到"otherButtonTitles:nil];
[alertView show];
NSLog(@"tempDic:%@",tempDic);
returnNO;
}
returnYES;
}
相关文章推荐
- ios  与 js 交互 stringByEvaluatingJavaScriptFromString的使用方法
- js 与ios 交互的三种方法
- iOS: JS和Native交互的两种方法,iosjsnative交互
- iOS JS 交互之利用系统JSContext实现 JS调用OC方法以及Objective-C调用JavaScript方法
- ios js与oc原生WKWebView方法注入及交互传值
- Cordova - 与iOS原生代码交互1(通过JS调用Swift方法)
- iOS JS 交互之利用系统JSContext实现 JS调用oc方法
- Android 和 IOS 使用 jsBridge 进行交互方法
- iOS: JS和Native交互的两种方法,iosjsnative交互
- iOS和JS的交互之在代理方法拦截Url,识别判断
- iOS用WKWebView与JS交互获取系统图片及WKWebView的Alert,Confirm,TextInput的监听代理方法使用,屏蔽WebView的可选菜单
- iOS开发 - OC之JS和Native交互的两种方法,ios js与native交互
- iOS之oc与html之间的交互(oc中调用js的方法)
- iOS JS 交互之利用系统JSContext实现 JS调用oc方法
- iOS用WKWebView与JS交互获取系统图片及WKWebView的Alert,Confirm,TextInput的监听代理方法使用,屏蔽WebView的可选菜单
- iOS 几种webView与JS交互的方法
- iOS与JS交互,OC调用JS方法,JS调用OC方法,URL解码
- iOS: JS和Native交互的两种方法
- JS与android、IOS 页面交互时,js错误信息捕获方法
- iOS: JS和Native交互的两种方法