簡單整理下在Oc編譯環(huán)境下如何與html文件進行交互,本文介紹兩種比較簡單的交互方式
1.使用UIWebview代理方法攔截Url
2.使用JavaScriptCore框架進行操作
第一種方法 —— 攔截UIWebview中url
1.導入html文件恕稠,并進行相關編寫操作
2.加載UIWebView并設置代理
// 找到本地html文件并加載UIWebView
_webView.backgroundColor = [UIColor redColor];
_webView.delegate = self;
NSString *path = [[NSBundle mainBundle] pathForResource:@"First" ofType:@"html"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:path]];
[_webView loadRequest:request];
3.交互操作
1.調用JS
NSString *jsStr = [NSString stringWithFormat:@"showAlert('%@')",@"這里是JS中alert彈出的message"];
[_webView stringByEvaluatingJavaScriptFromString:jsStr];
// 向js方法中傳入參數時二驰,方法名不能寫錯 showAlert
2.代理方法攔截Url
#pragma mark - UIWebViewDelegate HTMl 交互OC
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL * url = [request URL];
// url ==>firstclick://shareClick?title=%E5%88%86%E4%BA%AB%E7%9A%84%E6%A0%87%E9%A2%98&content=%E5%88%86%E4%BA%AB%E7%9A%84%E5%86%85%E5%AE%B9&url=%E9%93%BE%E6%8E%A5%E5%9C%B0%E5%9D%80&imagePath=%E5%9B%BE%E7%89%87%E5%9C%B0%E5%9D%80
if ([[url scheme] isEqualToString:@"firstclick"]) {
// [url scheme] ==> firstclick
NSArray *params =[url.query componentsSeparatedByString:@"&"];
NSMutableDictionary *tempDic = [NSMutableDictionary dictionary];
for (NSString *paramStr in params) {
NSArray *dicArray = [paramStr componentsSeparatedByString:@"="];
if (dicArray.count > 1) {
NSString *decodeValue = [dicArray[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[tempDic setObject:decodeValue forKey:dicArray[0]];
}
}
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"方式一" message:@"這是OC原生的彈出窗" delegate:self cancelButtonTitle:@"收到" otherButtonTitles:nil];
[alertView show];
/*
把從js文件中拿到的數據分割處理成字典驶悟,保存到本地
拿到js中的內容tempDic:{
content = "\U5206\U4eab\U7684\U5185\U5bb9";
imagePath = "\U56fe\U7247\U5730\U5740";
title = "\U5206\U4eab\U7684\U6807\U9898";
url = "\U94fe\U63a5\U5730\U5740";
}
*/
NSLog(@"拿到js中的內容tempDic:%@",tempDic);
return NO;
}
return YES;
}
第二種方法 —— JavaScriptCore框架進行操作
1.導入html文件
2.導入JavaScriptCore.framework框架,并在項目導入頭文件
3.找到本地html文件加載UIWebview
NSString *path = [[NSBundle mainBundle] pathForResource:@"Second" ofType:@"html"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:path]];
[_webView loadRequest:request];
4.關鍵步驟 —— 交互操作
// 1.獲取運行環(huán)境
JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
//2.定義好JS要調用的方法, share就是調用的share方法名
context[@"share"] = ^() {
NSLog(@"+++++++Begin Log+++++++");
NSArray *args = [JSContext currentArguments]; // 找到所用對應的參數內容
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"方式二" message:@"這是OC原生的彈出窗" delegate:self cancelButtonTitle:@"收到" otherButtonTitles:nil];
[alertView show];
});
// 3.列舉出該方法中的JavaScript對象
for (JSValue *jsVal in args) {
NSLog(@"%@", jsVal.toString);
}
NSLog(@"-------End Log-------");
};
調用JS中的方法
JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
NSString *textJS = @"showAlert('這里是JS中alert彈出的message')";
[context evaluateScript:textJS];
以上簡單整理JS與OC交互的相關操作畸裳。