酷店

KDPWebViewViewController.m 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // KDPWebViewViewController.m
  3. // KuDianProject
  4. //
  5. // Created by admin on 2019/7/8.
  6. // Copyright © 2019 KDP. All rights reserved.
  7. //
  8. #import "KDPWebViewViewController.h"
  9. #import <WebKit/WebKit.h>
  10. @interface KDPWebViewViewController ()<WKUIDelegate,WKNavigationDelegate>
  11. @property (nonatomic, strong) WKWebView *webView;
  12. @property (nonatomic, strong) UIProgressView *processView;
  13. @property (nonatomic, strong) UIButton *backButton;
  14. @end
  15. @implementation KDPWebViewViewController
  16. - (void)viewDidLoad {
  17. [super viewDidLoad];
  18. // Do any additional setup after loading the view.
  19. [self setContentView];
  20. [self loadWebPage];
  21. }
  22. - (void)setContentView{
  23. self.navBar.backgroundColor = [UIColor whiteColor];
  24. self.navBar.navTitleLabel.textColor = [UIColor colorWithHex:0x333333];
  25. // self.backButton = [UIButton buttonWithType:UIButtonTypeSystem];
  26. // [self.backButton setImage:[UIImage imageNamed:@"return_black"] forState:UIControlStateNormal];
  27. // self.backButton.frame = CGRectMake(0, 0, 40, 40);
  28. // [self.backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
  29. // [self.navBar addSubview:self.backButton];
  30. [self.navBar addleftReturnButton:self selector:@selector(backAction:)];
  31. WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
  32. config.allowsInlineMediaPlayback = NO;
  33. self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, KDNavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-KDNavBarHeight) configuration:config];
  34. self.webView.backgroundColor = [UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0];
  35. self.webView.navigationDelegate = self;
  36. self.webView.UIDelegate = self;
  37. //使用kvo监听进度
  38. [self.webView addObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress)) options:0 context:nil];
  39. //手势触摸滑动
  40. self.webView.allowsBackForwardNavigationGestures = YES;
  41. //自适应
  42. [self.webView sizeToFit];
  43. [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.webUrl]]];
  44. [self.view addSubview:self.webView];
  45. self.processView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, KDNavBarHeight, SCREEN_WIDTH, 2)];
  46. [self.processView setTrackTintColor:[UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0]];
  47. self.processView.progressTintColor = [UIColor baseColor];
  48. [self.view addSubview:self.processView];
  49. }
  50. - (void)loadWebPage{
  51. if (@available(iOS 9.0, *)) {
  52. NSArray * types=@[WKWebsiteDataTypeCookies,WKWebsiteDataTypeLocalStorage];
  53. NSSet *websiteDataTypes= [NSSet setWithArray:types];
  54. NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
  55. [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
  56. }];
  57. }
  58. // NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.url]];
  59. NSString *urlStr = [self.webUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  60. NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr]
  61. cachePolicy:NSURLRequestReloadIgnoringCacheData
  62. timeoutInterval:15.0];
  63. [self.webView loadRequest:theRequest];
  64. }
  65. - (void)setWebUrl:(NSString *)webUrl{
  66. _webUrl = webUrl;
  67. }
  68. - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
  69. //开始加载的时候,让进度条显示
  70. self.processView.hidden = NO;
  71. self.processView.progress = 0;
  72. self.processView.alpha = 1.0;
  73. [UIView animateWithDuration:0.8 animations:^{
  74. self.processView.progress = 0.6;
  75. }];
  76. }
  77. - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
  78. NSURL *url = navigationAction.request.URL;
  79. UIApplication *app = [UIApplication sharedApplication];
  80. // 打开appstore
  81. if ([url.absoluteString containsString:@"https://itunes.apple.com/cn/app/"])
  82. {
  83. if ([app canOpenURL:url])
  84. {
  85. //设备系统为IOS 10.0或者以上的
  86. if (@available(iOS 10.0, *)) {
  87. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  88. } else {
  89. [[UIApplication sharedApplication] openURL:url];
  90. }
  91. decisionHandler(WKNavigationActionPolicyCancel);
  92. return;
  93. }
  94. }
  95. decisionHandler(WKNavigationActionPolicyAllow);
  96. }
  97. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
  98. self.navBar.navTitleLabel.text = webView.title;
  99. }
  100. -(void)viewWillAppear:(BOOL)animated
  101. {
  102. [super viewWillAppear:animated];
  103. [UIApplication sharedApplication].statusBarStyle=UIStatusBarStyleDefault;
  104. self.navBar.navTitleLabel.textColor = [UIColor whiteColor];
  105. }
  106. -(void)viewWillDisappear:(BOOL)animated
  107. {
  108. [super viewWillDisappear:animated];
  109. [UIApplication sharedApplication].statusBarStyle=UIStatusBarStyleLightContent;
  110. }
  111. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
  112. if ([keyPath isEqualToString:NSStringFromSelector(@selector(estimatedProgress))] && object == self.webView ) {
  113. [self.processView setAlpha:1.0];
  114. [self.processView setProgress:self.webView.estimatedProgress ];
  115. if (self.webView.estimatedProgress >= 1.0) {
  116. [UIView animateWithDuration:0.7 animations:^{
  117. [self.processView setProgress:1.0 animated:YES];
  118. [self.processView setAlpha:0.0];
  119. }];
  120. }
  121. }else{
  122. [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  123. }
  124. }
  125. - (void)backAction:(UIButton *)sender{
  126. if (self.presentingViewController) {
  127. [self dismissViewControllerAnimated:YES completion:nil];
  128. } else{
  129. [self.navigationController popViewControllerAnimated:YES];
  130. }
  131. }
  132. //观察的移除
  133. - (void)dealloc{
  134. [self.webView removeObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress))];
  135. [[NSNotificationCenter defaultCenter] removeObserver:self];
  136. }
  137. @end