BogoPrivacyPopView.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // BogoPrivacyPopView.m
  3. // BuguLive
  4. //
  5. // Created by Mac on 2021/9/13.
  6. // Copyright © 2021 xfg. All rights reserved.
  7. //
  8. #import "BogoPrivacyPopView.h"
  9. #import "MLEmojiLabel.h"
  10. @interface BogoPrivacyPopView ()<MLEmojiLabelDelegate,WKNavigationDelegate>
  11. @property(nonatomic, strong) WKWebView *webView;
  12. @property(nonatomic, strong) UIView *shadowView;
  13. @property (weak, nonatomic) IBOutlet MLEmojiLabel *tipLabel;
  14. @property (weak, nonatomic) IBOutlet UILabel *tipTop;
  15. @end
  16. @implementation BogoPrivacyPopView
  17. - (void)awakeFromNib{
  18. [super awakeFromNib];
  19. self.hidden = YES;
  20. self.frame = CGRectMake(50, kScreenH, kScreenW - 100, 392);
  21. self.layer.cornerRadius = 10;
  22. self.clipsToBounds = YES;
  23. [self addSubview:self.webView];
  24. self.tipTop.text = ASLocalizedString(@"温馨提示");
  25. NSString *string = ASLocalizedString(@"点击同意即表示您已阅读并同意《平台用户协议》和《隐私协议》。");
  26. NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:string];
  27. [attr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#5C8BFB"] range:[string rangeOfString:ASLocalizedString(@"《平台用户协议》")]];
  28. [attr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#5C8BFB"] range:[string rangeOfString:ASLocalizedString(@"《隐私协议》")]];
  29. [self.tipLabel setAttributedText:attr];
  30. [self.tipLabel addLinkWithTextCheckingResult:[NSTextCheckingResult spellCheckingResultWithRange:[string rangeOfString:ASLocalizedString(@"《平台用户协议》")]] attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#5C8BFB"]}];
  31. [self.tipLabel addLinkWithTextCheckingResult:[NSTextCheckingResult spellCheckingResultWithRange:[string rangeOfString:ASLocalizedString(@"《隐私协议》")]] attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#5C8BFB"]}];
  32. self.tipLabel.delegate = self;
  33. [self.disagreeBtn setTitle:ASLocalizedString(@"不同意") forState:UIControlStateNormal];
  34. [self.agreeBtn setTitle:ASLocalizedString(@"同意") forState:UIControlStateNormal];
  35. }
  36. #pragma mark - MLEmojiLabelDelegate
  37. - (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithTextCheckingResult:(NSTextCheckingResult *)result{
  38. if (result.range.length == ASLocalizedString(@"《平台用户协议》").length) {
  39. if (self.delegate && [self.delegate respondsToSelector:@selector(privacyPopView:didClickUserAgreement:)]) {
  40. [self.delegate privacyPopView:self didClickUserAgreement:nil];
  41. }
  42. }else if (result.range.length == ASLocalizedString(@"《隐私协议》").length){
  43. if (self.delegate && [self.delegate respondsToSelector:@selector(privacyPopView:didClickPrivacyAgreement:)]) {
  44. [self.delegate privacyPopView:self didClickPrivacyAgreement:nil];
  45. }
  46. }
  47. }
  48. - (void)setUrl:(NSString *)url{
  49. if ([url hasPrefix:@"http"]) {
  50. [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
  51. }else{
  52. [self.webView loadHTMLString:url baseURL:nil];
  53. }
  54. }
  55. - (IBAction)refuseBtnAction:(UIButton *)sender {
  56. [self hide];
  57. abort();
  58. }
  59. - (IBAction)agreeBtnAction:(UIButton *)sender {
  60. [self hide];
  61. if (self.delegate && [self.delegate respondsToSelector:@selector(privacyPopView:didClickAgreeBtn:)]) {
  62. [self.delegate privacyPopView:self didClickAgreeBtn:sender];
  63. }
  64. }
  65. - (WKWebView *)webView{
  66. if (!_webView) {
  67. NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
  68. WKUserScript *wkUScript = [[WKUserScript alloc]initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
  69. WKUserContentController *wkUController = [WKUserContentController new];
  70. [wkUController addUserScript:wkUScript];
  71. WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc]init];
  72. config.userContentController = wkUController;
  73. _webView = [[WKWebView alloc]initWithFrame:CGRectMake(15, 50, kScreenW - 130, 230) configuration:config];
  74. _webView.UIDelegate = self;
  75. _webView.navigationDelegate = self;
  76. _webView.scrollView.showsVerticalScrollIndicator = NO;
  77. _webView.scrollView.showsHorizontalScrollIndicator = NO;
  78. }
  79. return _webView;
  80. }
  81. - (void)show:(UIView *)superView{
  82. [superView addSubview:self.shadowView];
  83. [superView addSubview:self];
  84. __weak __typeof(self)weakSelf = self;
  85. [UIView animateWithDuration:0.25 animations:^{
  86. __strong __typeof(weakSelf)strongSelf = weakSelf;
  87. CGFloat offsetY = ( superView.fd_height - strongSelf.fd_height ) / 2;
  88. strongSelf.frame = CGRectMake(strongSelf.fd_left, offsetY, strongSelf.fd_width, strongSelf.fd_height);
  89. }];
  90. }
  91. - (void)hide{
  92. __weak __typeof(self)weakSelf = self;
  93. CGFloat offsetY = self.superview.fd_height;
  94. [UIView animateWithDuration:0.25 animations:^{
  95. __strong __typeof(weakSelf)strongSelf = weakSelf;
  96. strongSelf.frame = CGRectMake(strongSelf.fd_left, offsetY, strongSelf.fd_width, strongSelf.fd_height);
  97. } completion:^(BOOL finished) {
  98. __strong __typeof(weakSelf)strongSelf = weakSelf;
  99. [strongSelf.shadowView removeFromSuperview];
  100. [strongSelf removeFromSuperview];
  101. }];
  102. }
  103. - (UIView *)shadowView{
  104. if (!_shadowView) {
  105. _shadowView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, FD_ScreenWidth, FD_ScreenHeight)];
  106. _shadowView.backgroundColor = [FD_BlackColor colorWithAlphaComponent:0.4];
  107. _shadowView.hidden = YES;
  108. }
  109. return _shadowView;
  110. }
  111. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
  112. self.shadowView.hidden = NO;
  113. self.hidden = NO;
  114. [webView evaluateJavaScript:@"document.body.scrollWidth"completionHandler:^(id _Nullable result,NSError * _Nullable error){
  115. CGFloat ratio = CGRectGetWidth(webView.frame) /[result floatValue];
  116. NSLog(@"scrollWidth高度:%.2f",[result floatValue]);
  117. [webView evaluateJavaScript:@"document.body.scrollHeight"completionHandler:^(id _Nullable result,NSError * _Nullable error){
  118. NSLog(@"scrollHeight高度:%.2f",[result floatValue]*ratio);
  119. CGFloat height = [result floatValue]*ratio;
  120. self.webView.frame = CGRectMake(15, 50, FD_ScreenWidth - 130, height - 10);
  121. self.webView.scrollView.frame = CGRectMake(0, 0, FD_ScreenWidth - 130, height);
  122. self.webView.scrollView.contentSize = CGSizeMake(FD_ScreenWidth - 130, height);
  123. self.bounds = CGRectMake(0, 0, kScreenW - 100, 152 + height);
  124. }];
  125. }];
  126. }
  127. @end