KWMMidDetailView.m 4.88 KB
Newer Older
houweibin committed
1 2 3 4 5 6 7 8
//
//  KWMMidDetailView.m
//  iCemarose
//
//  Created by HouWeiBin on 2017/7/13.
//  Copyright © 2017年 kollway. All rights reserved.
//

9 10
#import <WebKit/WebKit.h>

houweibin committed
11 12
#import "KWMMidDetailView.h"
#import "KWMStringUtil.h"
13 14 15 16 17 18 19 20 21
#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;

houweibin committed
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

@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{
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
//    [[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]];
houweibin committed
83 84 85
    
}

86 87 88 89 90
//- (void)layoutSubviews {
//    [super layoutSubviews];
//    self.webView.frame = self.bounds;
//}

houweibin committed
91 92
-(void)setProduct:(BUYProduct *)product{
    _product = product;
93 94 95
    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];
u  
lee committed
96 97
        NSString *css = [NSString stringWithFormat:@"p:empty{margin: 0 20px;}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;for(var e of document.getElementsByTagName('p')) { if(e.getElementsByTagName('img').length==0) e.style.setProperty('margin','0 20px') }</script><style type=\"text/css\">%@</style>",product.htmlDescription,css] stringByReplacingOccurrencesOfString:@"src=\"//cdn.shopify.com" withString:@"src=\"https://o42yton8r.qnssl.com"];
98 99
        [self.webView loadHTMLString:html baseURL:nil];
        
houweibin committed
100
    }else{
101 102 103
//        self.lbDetail.text = @"暂无商品描述";
        self.hidden = YES;
        [self.delegate kwm_updatedHeight:0];
houweibin committed
104 105 106
    }
}

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
- (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];
}
houweibin committed
122 123

@end