1.加載網(wǎng)頁
NSString *path = @"http://jinwan.c.jinerkan.com/v3/client/video";
NSURL *url = [[NSURL alloc] initWithString:path];
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];
2.與Html交互
NSURL * url;
NSURLRequest * request;
NSString * stringPath;
NSString * stringHtml;
//后退
[_webView goBack];
//前進
[_webView goForward];
//刷新
[_webView reload];
//停止
[_webView stopLoading];
request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
//加載HTLM文件
//找到本地html文件
stringPath = [[NSBundle mainBundle]pathForResource:@"Test" ofType:@"html"];
//得到html原碼字符串形式
stringHtml = [NSString stringWithContentsOfFile:stringPath encoding:NSUTF8StringEncoding error:nil];
//加載html源碼
[_webView loadHTMLString:stringHtml baseURL:nil];
//與JavaScript交互
兩種方向
1.iOS客戶端調(diào)用和html中的事件
2.html中的js事件觸發(fā)iOS客戶端中的方法
//參數(shù):即為html中的js事件
[_webView stringByEvaluatingJavaScriptFromString:@"show()"];
pragma mark -- webViewDelegateMethod
- 開始加載請求
- @param webView 當(dāng)前web
- @param request 請求體
- @param navigationType 導(dǎo)航欄樣式
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//得到當(dāng)前網(wǎng)頁網(wǎng)址
NSString * url = request.URL.absoluteString;
//
_textField.text = url;
pragma mark html中js事件為修改當(dāng)前網(wǎng)頁的網(wǎng)址(window.location.href="oc://alert")即會調(diào)用該代理的方法,因此該代理方法即為html與ios客戶端的連接
//首先判斷為那個客戶端(通過oc標(biāo)識判斷)
//字符串分割(分割為客戶端類型與所需調(diào)用的方法)
NSArray * array = [url componentsSeparatedByString:@"http://"];
if ([[array firstObject]hasPrefix:@"oc"]) {
//說明為iOS客戶端所需調(diào)用的方法
[self alert];
//根據(jù)方法名調(diào)用方法
//1.先將方法名存入選擇器中
SEL sel = NSSelectorFromString([array lastObject]);
//調(diào)用方法
//內(nèi)存警告問題
[self performSelector:sel];
}
return YES;
}