iOS加載本地HTML注意點(diǎn):
因?yàn)榍岸薐S CSS 的調(diào)用有嚴(yán)格的頁(yè)面結(jié)構(gòu) 所以我們導(dǎo)入文件的時(shí)候盡量選擇create folder reference!H恼蕖6拙鳌!
感謝以下這位指點(diǎn)迷津:
http://www.reibang.com/p/ee417ad66ad4
加載html方式:
1.第一種方式,使用loadRequest:方法加載本地文件NSURLRequest
NSString* path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSURL* url = [NSURL fileURLWithPath:path];
NSURLRequest* request = [NSURLRequest requestWithURL:url] ;
[webView loadRequest:request];
2.第二種方式,使用loadHTMLString:baseURL:加載HTML字符串
NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSString *html = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:html baseURL:baseURL];
3弃揽、第三種方法:
NSString *path = [[NSBundle mainBundle] pathForResource:@"test_map.html" ofType:nil];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL fileURLWithPath:path]];
[self.webView loadRequest:request];
補(bǔ)充交互:
1脯爪、早期的JS與原生交互:
/**
*? 獲取js交互事件
*? js事件里面加載了一個(gè)URL,在這個(gè)webView的代理方法中可以監(jiān)聽
*/
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL * url = [request URL];
if ([[url scheme] isEqualToString:@"firstclick"]) { // js里面之行的函數(shù)名(帶有大寫的也接收到小寫)
// 對(duì)html傳過(guò)來(lái)的參數(shù)做解析使用
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] stringByRemovingPercentEncoding];
[tempDic setObject:decodeValue forKey:dicArray[0]];
}
}
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"方式一" message:@"這是OC原生的彈出窗" delegate:self cancelButtonTitle:@"收到" otherButtonTitles:nil];
[alertView show];
}
return YES;
}
2矿微、iOS 7之后痕慢,apple添加了一個(gè)新的庫(kù)JavaScriptCore,用來(lái)做JS交互:
// iOS 7之后 js與oc交互
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// 在OC中獲取JS的上下文
JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
//定義好JS要調(diào)用的方法, share就是調(diào)用的share方法名
context[@"share"] = ^() {
// 監(jiān)聽到j(luò)s動(dòng)作 彈出oc提示框
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"方式二" message:@"這是OC原生的彈出窗" delegate:self cancelButtonTitle:@"收到" otherButtonTitles:nil];
[alertView show];
// 當(dāng)前調(diào)用該方法的對(duì)象
NSLog(@"對(duì)象---> %@", [JSContext currentThis]);
// JSContext提供了類方法來(lái)獲取參數(shù)列表
NSArray *args = [JSContext currentArguments];
for (JSValue *jsVal in args) {
// 獲取參數(shù)字符串
NSLog(@"jsVal.toString----> :%@", jsVal.toString);
}
};
}
3涌矢、 OC調(diào)用JS篇 (html在頁(yè)面上加載完成之后有效)
- (void)webViewDidFinishLoad:(UIWebView *)webView {
//方式一
/**? 注意:該方法會(huì)同步返回一個(gè)字符串掖举,因此是一個(gè)同步方法,可能會(huì)阻塞UI? */
// js函數(shù)名字showAlert(massage)
NSString *jsStr = [NSString stringWithFormat:@"showAlert('%@')",@"這里是JS中alert彈出的message"];
[self.webView stringByEvaluatingJavaScriptFromString:jsStr];
//方式二
/**? 使用JavaScriptCore框架實(shí)現(xiàn)oc調(diào)用js? */
JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
NSString *textJS = @"showAlert('這里是JS中alert彈出的message')";
[context evaluateScript:textJS];
}
4娜庇、使用第三方進(jìn)行交互:
常用的第三方如:
JSBridge
WebViewJavascriptBridge