WebViewController.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // WebViewController.m
  3. // 千模
  4. //
  5. // Created by Drew on 2018/12/19.
  6. // Copyright © 2018 MUMEI. All rights reserved.
  7. //
  8. #import "WebViewController.h"
  9. #import "Masonry.h"
  10. #import <WebKit/WebKit.h>
  11. #import <WXApi.h>
  12. #import "SharePopViewController.h"
  13. #import "WeakScriptMessageDelegate.h"
  14. #import "UploadVideoViewController.h"
  15. #import "UIImage+Common.h"
  16. #import "ModelFansViewController.h"
  17. @interface WebViewController () <ShareDelegate, WKUIDelegate, WKScriptMessageHandler, WKNavigationDelegate>
  18. @property(nonatomic, strong) WKWebView *webView;
  19. @end
  20. @implementation WebViewController
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_back"] style:UIBarButtonItemStylePlain target:self action:@selector(backClick)];
  24. self.navigationItem.leftBarButtonItem = leftItem;
  25. UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_share"] style:UIBarButtonItemStylePlain target:self action:@selector(share)];
  26. self.navigationItem.rightBarButtonItem = rightItem;
  27. self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
  28. self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
  29. self.navigationController.navigationBar.translucent = NO;
  30. WKWebView *webView = [[WKWebView alloc] init];
  31. [self.view addSubview:webView];
  32. [webView mas_makeConstraints:^(MASConstraintMaker *make) {
  33. make.left.equalTo(self.view.mas_left);
  34. make.right.equalTo(self.view.mas_right);
  35. make.top.equalTo(self.mas_topLayoutGuide);
  36. make.bottom.equalTo(self.mas_bottomLayoutGuide);
  37. }];
  38. webView.UIDelegate = self;
  39. webView.navigationDelegate = self;
  40. [[webView configuration].userContentController addScriptMessageHandler:[[WeakScriptMessageDelegate alloc] initWithDelegate:self] name:@"chmo"];
  41. [webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
  42. if ([self.url containsString:@"?"]) {
  43. [NSString stringWithFormat:@"%@&memberpk=%@&activitypk=%@", self.url, [ModelUser user].pk, self.activityPK];
  44. } else {
  45. self.url = [NSString stringWithFormat:@"%@?memberpk=%@&activitypk=%@", self.url, [ModelUser user].pk, self.activityPK];
  46. }
  47. if (self.modelPK) {
  48. self.url = [self.url stringByAppendingFormat:@"&modelpk=%@", self.modelPK];
  49. }
  50. [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
  51. self.webView = webView;
  52. }
  53. - (void)backClick {
  54. if ([self.webView canGoBack]) {
  55. [self.webView goBack];
  56. } else {
  57. [self.navigationController popViewControllerAnimated:YES];
  58. }
  59. }
  60. - (void)viewWillAppear:(BOOL)animated {
  61. [super viewWillAppear:animated];
  62. [self.navigationController setNavigationBarHidden:NO animated:YES];
  63. self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
  64. self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
  65. [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRed:1.0 green:64 / 255.f blue:149 / 255.f alpha:1.0]] forBarMetrics:UIBarMetricsDefault];
  66. [self.navigationController.navigationBar setShadowImage:[UIImage new]];
  67. self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
  68. self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
  69. }
  70. - (void)viewWillDisappear:(BOOL)animated {
  71. [super viewWillDisappear:animated];
  72. [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
  73. [self.navigationController.navigationBar setShadowImage:nil];
  74. self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
  75. self.navigationController.navigationBar.titleTextAttributes = nil;
  76. }
  77. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  78. if ([keyPath isEqualToString:@"title"]) {
  79. if (object == self.webView) {
  80. self.title = self.webView.title;
  81. } else {
  82. [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  83. }
  84. } else {
  85. [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  86. }
  87. }
  88. - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
  89. if ([@"tel" isEqualToString:navigationAction.request.URL.scheme]) {
  90. if (@available(iOS 10.0, *)) {
  91. [[UIApplication sharedApplication] openURL:navigationAction.request.URL options:@{} completionHandler:nil];
  92. } else {
  93. if (![[UIApplication sharedApplication] canOpenURL:navigationAction.request.URL]) {
  94. [[UIApplication sharedApplication] openURL:navigationAction.request.URL];
  95. }
  96. }
  97. decisionHandler(WKNavigationActionPolicyCancel);
  98. } else {
  99. decisionHandler(WKNavigationActionPolicyAllow);
  100. }
  101. }
  102. - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
  103. NSLog(@"name = %@, body = %@", message.name, message.body);
  104. NSString *action = message.body[@"action"];
  105. if ([@"signUp" isEqualToString:action]) {
  106. NSString *activitypk = message.body[@"activitypk"];
  107. UploadVideoViewController *vc = [[UploadVideoViewController alloc] init];
  108. vc.activitypk = activitypk;
  109. [self.navigationController pushViewController:vc animated:YES];
  110. } else if ([@"fansList" isEqualToString:action]) {
  111. NSString *modelpk = [message.body[@"modelpk"] stringValue];
  112. ModelFansViewController *modelFansVC = [[ModelFansViewController alloc] init];
  113. modelFansVC.modelPK = modelpk;
  114. modelFansVC.hidesBottomBarWhenPushed = YES;
  115. [self.navigationController pushViewController:modelFansVC animated:YES];
  116. }
  117. }
  118. - (void)share {
  119. SharePopViewController *shareVC = [[SharePopViewController alloc] init];
  120. shareVC.delegate = self;
  121. [self presentViewController:shareVC animated:NO completion:nil];
  122. }
  123. - (void)shareWxSession {
  124. [self shareToWechat:WXSceneSession withTitle:@"千模通告" description:self.title url:self.url];
  125. }
  126. - (void)shareWxTimeline {
  127. [self shareToWechat:WXSceneTimeline withTitle:self.title description:self.title url:self.url];
  128. }
  129. - (void)shareToWechat:(enum WXScene)scene withTitle:(NSString *)title description:(NSString *)description url:(NSString *)url {
  130. SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
  131. req.bText = NO;
  132. req.scene = scene;
  133. WXMediaMessage *urlMessage = [WXMediaMessage message];
  134. urlMessage.title = title;
  135. urlMessage.description = description;
  136. [urlMessage setThumbImage:[UIImage imageNamed:@"share_icon"]];
  137. WXWebpageObject *webObj = [WXWebpageObject object];
  138. webObj.webpageUrl = url;
  139. urlMessage.mediaObject = webObj;
  140. req.message = urlMessage;
  141. [WXApi sendReq:req];
  142. }
  143. - (void)dealloc {
  144. [[self.webView configuration].userContentController removeScriptMessageHandlerForName:@"chmo"];
  145. [self.webView removeObserver:self forKeyPath:@"title"];
  146. }
  147. @end