ASWebViewController.m 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // ASWebViewController.m
  3. // Asteria
  4. //
  5. // Created by iOS on 2023/6/24.
  6. //
  7. #import "ASWebViewController.h"
  8. @interface ASWebViewController ()<WKNavigationDelegate, WKUIDelegate>
  9. @property (nonatomic, strong) WKWebView *webView;
  10. @end
  11. @implementation ASWebViewController
  12. - (void)viewDidLoad {
  13. [super viewDidLoad];
  14. [self loadSubVs];
  15. [self configViews];
  16. [self beginLoad];
  17. }
  18. - (void)beginLoad {
  19. NSURL *url = [NSURL URLWithString:self.webUrl];
  20. if (!url) {
  21. return;
  22. }
  23. NSURLRequest *requst = [NSURLRequest requestWithURL:url];
  24. [self.webView loadRequest:requst];
  25. }
  26. - (void)configViews {
  27. [self setTitleStr:self.customTitle];
  28. [self setNavRightSearch:^{
  29. }];
  30. }
  31. - (void)loadSubVs {
  32. [self.view addSubview:self.webView ];
  33. [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
  34. make.top.equalTo(self.customNavBar.mas_bottom);
  35. make.bottom.leading.trailing.equalTo(self.view);
  36. }];
  37. }
  38. // MARK: - subvs
  39. - (WKWebView *)webView {
  40. if (!_webView) {
  41. WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
  42. WKWebView *v = [[WKWebView alloc] initWithFrame:CGRectZero configuration:config];
  43. v.navigationDelegate = self;
  44. v.UIDelegate = self;
  45. _webView = v;
  46. }
  47. return _webView;
  48. }
  49. // MARK: - uidelegate navidelegate
  50. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
  51. [webView evaluateJavaScript:@"document.title" completionHandler:^(id _Nullable title, NSError * _Nullable error) {
  52. NSString *titleStr = [NSString stringWithFormat:@"%@",title];
  53. if (!titleStr.isEmpty) {
  54. [self setTitleStr:titleStr];
  55. }
  56. }];
  57. }
  58. @end