// // 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