FDWebViewController.m 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //
  2. // FDWebViewController.m
  3. // Project
  4. //
  5. // Created by bogokj on 2020/5/20.
  6. // Copyright © 2020 fandong. All rights reserved.
  7. //
  8. #import "FDWebViewController.h"
  9. #import <WebKit/WebKit.h>
  10. #import "FDHUDManager.h"
  11. #import "FDWKUserContentController.h"
  12. #import <Masonry/Masonry.h>
  13. @interface FDWebViewController ()<WKNavigationDelegate,WKUIDelegate,WKScriptMessageHandler>
  14. @property(nonatomic, strong) WKWebView *webView;
  15. @property(nonatomic, strong) NSURL *url;
  16. @end
  17. @implementation FDWebViewController
  18. - (instancetype)initWithURL:(NSURL *)url{
  19. if (self = [super init]) {
  20. _url = url;
  21. if (_url) {
  22. [self.webView loadRequest:[NSURLRequest requestWithURL:_url]];
  23. }else{
  24. [[FDHUDManager defaultManager] show:@"未初始化地址" ToView:self.view];
  25. }
  26. }
  27. return self;
  28. }
  29. - (void)viewDidLoad {
  30. [super viewDidLoad];
  31. // Do any additional setup after loading the view.
  32. [self.view addSubview:self.webView];
  33. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onReceivewap_interceptNoti) name:@"wap_intercept" object:nil];
  34. [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
  35. make.edges.equalTo(self.view);
  36. }];
  37. }
  38. - (void)onReceivewap_interceptNoti{
  39. [self.navigationController popViewControllerAnimated:YES];
  40. }
  41. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
  42. self.title = webView.title;
  43. }
  44. - (WKWebView *)webView{
  45. if (!_webView) {
  46. NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
  47. WKUserScript *wkUScript = [[WKUserScript alloc]initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
  48. FDWKUserContentController *wkUController = [FDWKUserContentController new];
  49. // [wkUController addUserScript:wkUScript];
  50. WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc]init];
  51. config.userContentController = wkUController;
  52. _webView = [[WKWebView alloc]initWithFrame:CGRectZero configuration:config];
  53. _webView.UIDelegate = self;
  54. _webView.navigationDelegate = self;
  55. }
  56. return _webView;
  57. }
  58. @end