ASGoodsIntrouduceWebView.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // ASGoodsIntrouduceWebView.m
  3. // Asteria
  4. //
  5. // Created by xingyu on 2024/5/20.
  6. //
  7. #import "ASGoodsIntrouduceWebView.h"
  8. @interface ASGoodsIntrouduceWebView()<WKNavigationDelegate>
  9. @property (nonatomic, strong) WKWebView *wkWebView;
  10. @end
  11. @implementation ASGoodsIntrouduceWebView
  12. - (instancetype)initWithFrame:(CGRect)frame {
  13. if (self = [super initWithFrame:frame]) {
  14. [self addSubview:self.wkWebView];
  15. [self.wkWebView mas_makeConstraints:^(MASConstraintMaker *make) {
  16. make.edges.mas_equalTo(0);
  17. make.top.mas_equalTo(10);
  18. make.width.mas_equalTo(KScreenWidth);
  19. make.height.mas_equalTo(200);
  20. // make.bottom.mas_equalTo(-10);
  21. }];
  22. }
  23. return self;
  24. }
  25. - (void)loadWebUrlWithData:(GoodsInformationM *)model {
  26. NSString *product_details= @"";
  27. for (NSDictionary *dic in model.custom_attributes) {
  28. NSString *tmpStr= MM_str(dic[@"attribute_code"]);
  29. if ([tmpStr isEqualToString:@"feature"]) {
  30. product_details = MM_str(dic[@"value"]);
  31. }
  32. }
  33. NSString *htmlStr = [NSString stringWithFormat:@"<html> \n"
  34. "<head> \n"
  35. "<meta charset=\"utf-8\" /> \n"
  36. "<meta name=\"viewport\" content=\"width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no\" /> \n"
  37. "<style> \n"
  38. "*{ \n"
  39. "padding: 5; \n"
  40. "margin: 0; \n"
  41. "} \n"
  42. "table{ \n"
  43. "width: 100%%; \n"
  44. "border: 1px solid #A8A8A8; \n"
  45. "border-collapse:collapse; \n"
  46. "} \n"
  47. "th{ \n"
  48. "width: 30.42%%; \n"
  49. "min-height: 36px; \n"
  50. "padding: 10px; \n"
  51. "border: 1px solid #A8A8A8; \n"
  52. "font-size: 12px; \n"
  53. "font-weight: bold; \n"
  54. "color: #000; \n"
  55. "} \n"
  56. "td{ \n"
  57. "width: 69.58%%; \n"
  58. "min-height: 36px; \n"
  59. "padding: 10px; \n"
  60. "border: 1px solid #A8A8A8; \n"
  61. "font-size: 12px; \n"
  62. "font-weight: 400; \n"
  63. "color: #000; \n"
  64. "line-height: 16px; \n"
  65. "word-break: break-word; \n"
  66. "} \n"
  67. "</style> \n"
  68. "</head> \n"
  69. "<body style=\"font-family: -apple-system,Roboto,Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol',sans-serif;\">%@</body> \n"
  70. "</html>",product_details];
  71. [self.wkWebView loadHTMLString:htmlStr baseURL:nil];
  72. }
  73. #pragma mark WKNavigationDelegate 计算webView
  74. -(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
  75. @weakify(self)
  76. [webView evaluateJavaScript:@"document.body.offsetHeight" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
  77. @strongify(self)
  78. CGFloat height = [result doubleValue]+40;
  79. [self tool_webChangeFrame:height];
  80. }];
  81. }
  82. -(void)tool_webChangeFrame:(CGFloat)height{
  83. [self.wkWebView mas_updateConstraints:^(MASConstraintMaker *make) {
  84. make.height.mas_equalTo(height);
  85. }];
  86. if (self.loadFinishBlock) {
  87. self.loadFinishBlock(height);
  88. }
  89. }
  90. - (WKWebView *)wkWebView {
  91. if(!_wkWebView){
  92. NSString *injectionJSString = @"var script = document.createElement('meta');"
  93. "script.name = 'viewport';"
  94. "script.content=\"width=device-width, user-scalable=no\";"
  95. "document.getElementsByTagName('head')[0].appendChild(script);";
  96. // CSS选中样式取消 ---禁用web 长按菜单里的查询、学习、共享等按钮,
  97. NSString *css = @"body{-webkit-user-select:none;-webkit-user-drag:none;}";
  98. NSMutableString *javascript = [NSMutableString string];
  99. [javascript appendString:injectionJSString];
  100. [javascript appendString:@"var style = document.createElement('style');"];
  101. [javascript appendString:@"style.type = 'text/css';"];
  102. [javascript appendFormat:@"var cssContent = document.createTextNode('%@');", css];
  103. [javascript appendString:@"style.appendChild(cssContent);"];
  104. [javascript appendString:@"document.body.appendChild(style);"];
  105. WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:javascript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
  106. WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc]init];
  107. config.selectionGranularity = WKSelectionGranularityDynamic;
  108. WKUserContentController * wkUController = [[WKUserContentController alloc] init];
  109. [wkUController addUserScript:wkUScript];
  110. config.userContentController= wkUController;
  111. WKPreferences *wkp = [WKPreferences new];
  112. wkp.minimumFontSize = 12;
  113. config.preferences = wkp;
  114. _wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenWidth) configuration:config];
  115. _wkWebView.navigationDelegate=self;
  116. _wkWebView.backgroundColor=[UIColor colorWithHexString:@"#FFFFFF"];
  117. _wkWebView.scrollView.scrollEnabled=NO;
  118. _wkWebView.allowsLinkPreview = NO;
  119. if(@available(iOS 16.4 , *)){
  120. _wkWebView.inspectable = YES;
  121. }
  122. }
  123. return _wkWebView;
  124. }
  125. @end