| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 | ////  ASGoodsIntrouduceWebView.m//  Asteria////  Created by xingyu on 2024/5/20.//#import "ASGoodsIntrouduceWebView.h"@interface ASGoodsIntrouduceWebView()<WKNavigationDelegate>@property (nonatomic, strong) WKWebView *wkWebView;@end@implementation ASGoodsIntrouduceWebView- (instancetype)initWithFrame:(CGRect)frame {    if (self = [super initWithFrame:frame]) {                [self addSubview:self.wkWebView];        [self.wkWebView mas_makeConstraints:^(MASConstraintMaker *make) {            make.edges.mas_equalTo(0);            make.top.mas_equalTo(10);            make.width.mas_equalTo(KScreenWidth);            make.height.mas_equalTo(200);//            make.bottom.mas_equalTo(-10);        }];    }    return self;}- (void)loadWebUrlWithData:(GoodsInformationM *)model {    NSString *product_details= @"";    for (NSDictionary *dic in model.custom_attributes) {        NSString *tmpStr= MM_str(dic[@"attribute_code"]);        if ([tmpStr isEqualToString:@"feature"]) {            product_details = MM_str(dic[@"value"]);        }    }    NSString *htmlStr = [NSString stringWithFormat:@"<html> \n"                         "<head> \n"                         "<meta charset=\"utf-8\" /> \n"                         "<meta name=\"viewport\" content=\"width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no\" /> \n"                         "<style> \n"                             "*{ \n"                                 "padding: 5; \n"                                 "margin: 0; \n"                             "} \n"                             "table{ \n"                                 "width: 100%%; \n"                                 "border: 1px solid #A8A8A8; \n"                                 "border-collapse:collapse; \n"                             "} \n"                             "th{ \n"                                 "width: 30.42%%; \n"                                 "min-height: 36px; \n"                                 "padding: 10px; \n"                                 "border: 1px solid #A8A8A8; \n"                                 "font-size: 12px; \n"                                 "font-weight: bold; \n"                                 "color: #000; \n"                             "} \n"                             "td{ \n"                                 "width: 69.58%%; \n"                                 "min-height: 36px; \n"                                 "padding: 10px; \n"                                 "border: 1px solid #A8A8A8; \n"                                 "font-size: 12px; \n"                                 "font-weight: 400; \n"                                 "color: #000; \n"                                 "line-height: 16px; \n"                                 "word-break: break-word; \n"                             "} \n"                         "</style> \n"                         "</head> \n"                         "<body style=\"font-family: -apple-system,Roboto,Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol',sans-serif;\">%@</body> \n"                         "</html>",product_details];    [self.wkWebView loadHTMLString:htmlStr baseURL:nil];}#pragma mark WKNavigationDelegate 计算webView-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {    @weakify(self)    [webView evaluateJavaScript:@"document.body.offsetHeight" completionHandler:^(id _Nullable result, NSError * _Nullable error) {        @strongify(self)        CGFloat height = [result doubleValue]+40;        [self tool_webChangeFrame:height];    }];}-(void)tool_webChangeFrame:(CGFloat)height{    [self.wkWebView mas_updateConstraints:^(MASConstraintMaker *make) {        make.height.mas_equalTo(height);    }];        if (self.loadFinishBlock) {        self.loadFinishBlock(height);    }}- (WKWebView *)wkWebView {    if(!_wkWebView){        NSString *injectionJSString = @"var script = document.createElement('meta');"        "script.name = 'viewport';"        "script.content=\"width=device-width, user-scalable=no\";"        "document.getElementsByTagName('head')[0].appendChild(script);";                // CSS选中样式取消 ---禁用web 长按菜单里的查询、学习、共享等按钮,        NSString *css = @"body{-webkit-user-select:none;-webkit-user-drag:none;}";        NSMutableString *javascript = [NSMutableString string];        [javascript appendString:injectionJSString];        [javascript appendString:@"var style = document.createElement('style');"];        [javascript appendString:@"style.type = 'text/css';"];        [javascript appendFormat:@"var cssContent = document.createTextNode('%@');", css];        [javascript appendString:@"style.appendChild(cssContent);"];        [javascript appendString:@"document.body.appendChild(style);"];                WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:javascript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];                WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc]init];        config.selectionGranularity = WKSelectionGranularityDynamic;                WKUserContentController * wkUController = [[WKUserContentController alloc] init];                [wkUController addUserScript:wkUScript];                config.userContentController= wkUController;        WKPreferences *wkp = [WKPreferences new];        wkp.minimumFontSize = 12;        config.preferences = wkp;        _wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenWidth) configuration:config];        _wkWebView.navigationDelegate=self;        _wkWebView.backgroundColor=[UIColor colorWithHexString:@"#FFFFFF"];        _wkWebView.scrollView.scrollEnabled=NO;        _wkWebView.allowsLinkPreview = NO;        if(@available(iOS 16.4 , *)){            _wkWebView.inspectable = YES;        }    }    return _wkWebView;}@end
 |