加載本地H5
//設置wk配置項
WKWebViewConfiguration *Configuration = [[WKWebViewConfiguration alloc]init];
WKPreferences *Preferences = [[WKPreferences alloc]init];
Preferences.minimumFontSize = 40.0;
Preferences.javaScriptEnabled = YES;
Preferences.javaScriptCanOpenWindowsAutomatically = YES;
Configuration.preferences = Preferences;
//獲取本地H5
NSString *fileUrl = [[NSBundle mainBundle]pathForResource:@"index.html" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:fileUrl];
//創(chuàng)建wkWebView
self.wkWebView = [[WKWebView alloc]initWithFrame:self.view.bounds configuration:Configuration];
[self.wkWebView loadFileURL:url allowingReadAccessToURL:url];
self.wkWebView.UIDelegate = self;
self.wkWebView.navigationDelegate = self;
[self.view addSubview:self.wkWebView];
//監(jiān)聽progress loading title 三個屬性
[self.wkWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
[self.wkWebView addObserver:self forKeyPath:@"loading" options:NSKeyValueObservingOptionNew context:nil];
[self.wkWebView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
//設置progress
self.progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 2)];
self.progressView.progressTintColor = [UIColor redColor];
self.progressView.trackTintColor = [UIColor clearColor];
[self.view insertSubview:self.progressView aboveSubview:self.wkWebView];
設置useContent
//設置userContent 按鈕方法與H5一一對應
[self.wkWebView.configuration.userContentController addScriptMessageHandler:self name:@"ScanAction"];
[self.wkWebView.configuration.userContentController addScriptMessageHandler:self name:@"Location"];
[self.wkWebView.configuration.userContentController addScriptMessageHandler:self name:@"Share"];
[self.wkWebView.configuration.userContentController addScriptMessageHandler:self name:@"Color"];
[self.wkWebView.configuration.userContentController addScriptMessageHandler:self name:@"Pays"];
[self.wkWebView.configuration.userContentController addScriptMessageHandler:self name:@"Shake"];
[self.wkWebView.configuration.userContentController addScriptMessageHandler:self name:@"GoBack"];
[self.wkWebView.configuration.userContentController addScriptMessageHandler:self name:@"PlaySound"];
對應H5方法
function payClick() {
window.webkit.messageHandlers.Pays.postMessage({order_no:'201511120981234',channel:'wx',amount:1,subject:'粉色外套'});
}
WKScriptMessageHandler代理方法 js調用oc
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
if ([message.name isEqualToString:@"ScanAction"]) {
[self HandleScanAction];
}
}
oc注入js代碼 setLocation()為H5方法名
- (void)HandleScanAction
{
NSString *jsStr = [NSString stringWithFormat:@"setLocation('%@')",@"華為科技有限公司李小云"];
[self.wkWebView evaluateJavaScript:jsStr completionHandler:^(id _Nullable result, NSError * _Nullable error) {
NSLog(@"我調用完了");
}];
}
kvo監(jiān)聽方法
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
if ([object isKindOfClass:[WKWebView class]] && [keyPath isEqualToString:@"estimatedProgress"]) {
CGFloat newprogress = [[change objectForKey:NSKeyValueChangeNewKey] doubleValue];
if (newprogress == 1 ) {
[self.progressView setProgress:1.0 animated:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.progressView.hidden = YES;
});
}else{
[UIView animateWithDuration:5 animations:^{
self.progressView.hidden = NO;
[self.progressView setProgress:newprogress animated:YES];
}];
}
}else if([object isKindOfClass:[WKWebView class]] && [keyPath isEqualToString:@"loading"])
{
NSLog(@"%@",@"loading");
}else if([keyPath isEqualToString:@"title"]){
self.title = self.wkWebView.title;
}
//已經加載完畢了
if (!self.wkWebView.isLoading) {
NSString *jsStr = [NSString stringWithFormat:@"callJsAlert()"];
[self.wkWebView evaluateJavaScript:jsStr completionHandler:^(id _Nullable reslut, NSError * _Nullable error) {
NSLog(@"H5加載完畢");
}];
}
}
本文基本思路來自http://www.reibang.com/p/6ba2507445e4
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者