DYWKWebVC.m 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //
  2. // DYWKWebVC.m
  3. // DYAdAlertViewApp
  4. //
  5. // Created by Daniel Yao on 16/12/16.
  6. // Copyright © 2016年 Daniel Yao. All rights reserved.
  7. //
  8. #define ScreenWidth [UIScreen mainScreen].bounds.size.width
  9. #define ScreenHeight [UIScreen mainScreen].bounds.size.height
  10. #import "DYWKWebVC.h"
  11. #import <WebKit/WebKit.h>
  12. @interface DYWKWebVC ()<WKUIDelegate,WKNavigationDelegate>
  13. @property (nonatomic, strong) WKWebView *wkWebView;
  14. @property (weak, nonatomic) NSString *baseURLString;
  15. @end
  16. @implementation DYWKWebVC
  17. - (void)viewDidLoad {
  18. [super viewDidLoad];
  19. [self.view addSubview:self.wkWebView];
  20. [self.wkWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr]]];
  21. }
  22. #pragma mark - WKWebView WKNavigationDelegate 相关
  23. /// 是否允许加载网页 在发送请求之前,决定是否跳转
  24. - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
  25. NSString *urlString = [[navigationAction.request URL] absoluteString];
  26. urlString = [urlString stringByRemovingPercentEncoding];
  27. NSArray *urlComps = [urlString componentsSeparatedByString:@"://"];
  28. if ([urlComps count]) {
  29. // NSString *protocolHead = [urlComps objectAtIndex:0];
  30. }
  31. decisionHandler(WKNavigationActionPolicyAllow);
  32. }
  33. - (WKWebView *)wkWebView {
  34. if (_wkWebView == nil) {
  35. WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
  36. webView.navigationDelegate = self;
  37. // 允许左右划手势导航,默认允许
  38. webView.allowsBackForwardNavigationGestures = YES;
  39. _wkWebView = webView;
  40. }
  41. return _wkWebView;
  42. }
  43. @end