在开发iOS版的直播卖货系统源码过程中,如何在html上展示商品详情,下面小编将从WKWebView入手,来介绍下实现过程。
代码语言:javascript复制1、创建WKWebview
- (WKWebView *)webView{
if (!_webView) {
_webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, _window_width, _backScrollView.height)];
_webView.navigationDelegate = self;
_webView.opaque = NO;
_webView.multipleTouchEnabled = YES;
_webView.scrollView.delegate = self;
_webView.scrollView.bounces = NO;
_webView.scrollView.showsVerticalScrollIndicator = NO;
_webView.scrollView.scrollEnabled = NO;
_webView.scrollView.panGestureRecognizer.enabled = NO;
}
return _webView;
}
2、获取服务端返回的html的字符串
代码语言:javascript复制 NSString *description = minstr([storeInfo valueForKey:@“description"]);
//我这里更改一下html的格式,让其符合手机端展示,如果服务端返回的就是手机端展示的样式,这就可以不用更改 直接用就可以了
NSString * htmlStyle = @" <style type="text/css"> *{min-width: 0% !important;max-width: 100% !important;} table{ width: 100% !important;} img{ height: auto !important;} </style> ";
description = [htmlStyle stringByAppendingString:description];
NSString *aaa = @"<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">";
description = [aaa stringByAppendingString:description];
[_webView loadHTMLString:description baseURL:nil];
3、在WKWebView加载完成的代理方法中更改webView的frame
代码语言:javascript复制- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
[webView evaluateJavaScript:@"document.body.scrollHeight" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
//更改webview的frame
_webView.frame = CGRectMake(0, _goodListView.bottom, _window_width, [result doubleValue]);//将WKWebView的高度设置为内容高度
//更改背景scrollview的滑动范围
_backScrollView.contentSize = CGSizeMake(0, _webView.bottom);
}];
// 插入js代码,对图片进行点击操作
[webView evaluateJavaScript:@"function assignImageClickAction(){var imgs=document.getElementsByTagName('img');var length=imgs.length;for(var i=0; i < length;i ){img=imgs[i];if("ad" ==img.getAttribute("flag")){var parent = this.parentNode;if(parent.nodeName.toLowerCase() != "a")return;}img.onclick=function(){window.location.href='image-preview:' this.src}}}" completionHandler:^(id object, NSError *error) {
}];
[webView evaluateJavaScript:@"assignImageClickAction();" completionHandler:^(id object, NSError *error) {
}];
imageArray = [self getImgs:description];
}
#pragma mark -- 获取文章中的图片个数
- (NSMutableArray *)getImgs:(NSString *)string
{
NSMutableArray *arrImgURL = [[NSMutableArray alloc] init];
NSArray *array = [string componentsSeparatedByString:@"<img"];
NSInteger count = [array count] - 1;
for (int i = 0; i < count; i ) {
NSString *jsString = [NSString stringWithFormat:@"document.getElementsByTagName('img')[%d].src", i];
[_webView evaluateJavaScript:jsString completionHandler:^(NSString *str, NSError *error) {
if (error ==nil) {
[arrImgURL addObject:str];
}
}];
}
imageArray = [NSMutableArray arrayWithArray:arrImgURL];
return arrImgURL;
}
4、在WKWebView的代理方法中拦截图片添加的点击方法,可使用HZPhotoBrowser来展示图片
代码语言:javascript复制- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
NSURLRequest *request = navigationAction.request;
if ([request.URL.scheme isEqualToString: @"image-preview"])
{
NSString *url = [request.URL.absoluteString substringFromIndex:14];
//启动图片浏览器, 跳转到图片浏览页面
if (imageArray.count != 0) {
HZPhotoBrowser *browserVc = [[HZPhotoBrowser alloc] init];
browserVc.imageArray = imageArray;
browserVc.imageCount = imageArray.count; // 图片总数
browserVc.currentImageIndex = (int)[imageArray indexOfObject:url];//当前点击的图片
[browserVc show];
}
decisionHandler(WKNavigationActionPolicyAllow);
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
NSLog(@"在发送请求之前:%@",navigationAction.request.URL.absoluteString);
以上,就是在开发直播卖货系统源码时,利用WKWebView展示html格式的商品详情的过程。
声明:以上内容为作者本人原创,未经作者本人同意,禁止转载,否则将追究相关法律责任。