1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
// KWMMidDetailView.m
// iCemarose
//
// Created by HouWeiBin on 2017/7/13.
// Copyright © 2017年 kollway. All rights reserved.
//
#import <WebKit/WebKit.h>
#import "KWMMidDetailView.h"
#import "KWMStringUtil.h"
#import "KWMTBVSectionHeardView.h"
#import <JLRoutes/JLRoutes.h>
@interface KWMMidDetailView()<WKNavigationDelegate,WKScriptMessageHandler>
@property(nonatomic,weak) IBOutlet UILabel *lbDetail;
@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, strong) KWMTBVSectionHeardView *titleView;
@end
@implementation KWMMidDetailView
- (id)init{
if (self=[super init]){
[self addView];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self addView];
}
return self;
}
-(instancetype)initWithFrame:(CGRect)frame{
if (self =[super initWithFrame:frame]) {
[self addView];
}
return self;
}
-(void)awakeFromNib{
[super awakeFromNib];
}
-(void)addView{
// [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class])
// owner:self
// options:nil];
self.titleView = [[KWMTBVSectionHeardView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 50)];
self.titleView.title = @"商品详情";
self.titleView.ivMore.hidden = YES;
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
[userContentController addScriptMessageHandler:self name:@"handle"];
WKWebViewConfiguration *configutation = [[WKWebViewConfiguration alloc] init];
configutation.userContentController = userContentController;
self.webView = [[WKWebView alloc] initWithFrame:self.bounds configuration:configutation];
self.webView.navigationDelegate = self;
// self.vView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
// [self addSubview:self.vView];
[self addSubview:self.webView];
[self addSubview:self.titleView];
self.webView.translatesAutoresizingMaskIntoConstraints = NO;
self.titleView.translatesAutoresizingMaskIntoConstraints = NO;
id views = @{@"web":self.webView,@"title":self.titleView};
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[web]|" options:0 metrics:nil views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[title]|" options:0 metrics:nil views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[title(50)][web]|" options:0 metrics:nil views:views]];
}
//- (void)layoutSubviews {
// [super layoutSubviews];
// self.webView.frame = self.bounds;
//}
-(void)setProduct:(BUYProduct *)product{
_product = product;
if(product && product.htmlDescription.length){
// self.lbDetail.text = product.htmlDescription;
// NSString *html = [NSString stringWithFormat:@"%@<script type='text/javascript'>window.webkit.messageHandlers.handle.postMessage(document.body.clientHeight)</scriot>",product.htmlDescription];
NSString *css = [NSString stringWithFormat:@"p{font-size:12px; }img{width:100%%;}*{padding:0;margin:0;max-width:%f}",UI_SCREEN_WIDTH];
NSString *html = [[NSString stringWithFormat:@"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0 user-scalable=yes\" /><div id=\"container\">%@<div><script type=\"text/javascript\">function update() {window.webkit.messageHandlers.handle.postMessage({height:document.body.scrollHeight})} function timer(){ setTimeout(timer,1000); update()} function delay() { setTimeout(update,1000) } window.onload = update</script><style type=\"text/css\">%@</style>",product.htmlDescription,css] stringByReplacingOccurrencesOfString:@"src=\"//cdn.shopify.com" withString:@"src=\"https://o42yton8r.qnssl.com"];
[self.webView loadHTMLString:html baseURL:nil];
}else{
// self.lbDetail.text = @"暂无商品描述";
self.hidden = YES;
[self.delegate kwm_updatedHeight:0];
}
}
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSURL *url = navigationAction.request.URL;
if ([JLRoutes canRouteURL:url]) {
[JLRoutes routeURL:url];
decisionHandler(WKNavigationActionPolicyCancel);
}else {
decisionHandler(WKNavigationActionPolicyAllow);
}
}
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
NSNumber *height = [message.body objectForKey:@"height"];
NSLog(@"fuck height: %@",height);
[self.delegate kwm_updatedHeight:height.floatValue + 50];
}
@end