// // ASGoodsIntrouduceWebView.m // Asteria // // Created by xingyu on 2024/5/20. // #import "ASGoodsIntrouduceWebView.h" @interface ASGoodsIntrouduceWebView() @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:@" \n" " \n" " \n" " \n" " \n" " \n" "%@ \n" "",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