JXChatWebView.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // JXChatWebView.m
  3. // shiku_im
  4. //
  5. // Created by 123 on 2020/6/3.
  6. // Copyright © 2020 Reese. All rights reserved.
  7. //
  8. #import "JXChatWebView.h"
  9. #import "CYWebAddPointVC.h"
  10. #import <WebKit/WebKit.h>
  11. #import "UIView+LK.h"
  12. @interface JXChatWebView ()<WKUIDelegate,WKNavigationDelegate,JXRoomObjectDelegate>
  13. @property (nonatomic,strong)WKWebView *wkWebView;
  14. @property (nonatomic,weak) NSString *linkName1;
  15. @property (nonatomic,weak) NSString *linkURL1;
  16. @property (nonatomic,weak) NSString *imgUrl1;
  17. @property (nonatomic,weak) NSString *linkName2;
  18. @property (nonatomic,weak) NSString *linkURL2;
  19. @property (nonatomic,weak) NSString *imgUrl2;
  20. @property (nonatomic,assign)int vcnum;
  21. @property (nonatomic,weak) UIView *titlesView;
  22. @property (nonatomic,weak) UIView *indicatorView;
  23. @property (nonatomic,weak) UIButton *button_n;
  24. @property (nonatomic,weak) UIButton *selectedButton;
  25. @end
  26. @implementation JXChatWebView
  27. #pragma mark 接受新消息广播
  28. - (UIStatusBarStyle)preferredStatusBarStyle{
  29. return UIStatusBarStyleDefault;
  30. }
  31. -(void)newMsgSend:(NSNotification *)notifacation
  32. {
  33. self.countTipL.hidden=NO;
  34. }
  35. -(void)newRequest:(NSNotification *)notifacation
  36. {
  37. self.countTipL.hidden=NO;
  38. }
  39. -(void)newMsgCome:(NSNotification *)notifacation
  40. {
  41. self.countTipL.hidden=NO;
  42. }
  43. - (id)init
  44. {
  45. self = [super init];
  46. if (self) {
  47. [g_notify addObserver:self selector:@selector(newMsgCome:) name:kXMPPNewMsgNotifaction object:nil];//收到了一条新消息
  48. [g_notify addObserver:self selector:@selector(newMsgSend:) name:kXMPPMyLastSendNotifaction object:nil];//发送了一条消息
  49. [g_notify addObserver:self selector:@selector(newRequest:) name:kXMPPNewRequestNotifaction object:nil];//收到了一个好友验证类消息
  50. }
  51. return self;
  52. }
  53. - (void)viewDidLoad {
  54. [super viewDidLoad];
  55. // Do any additional setup after loading the view.
  56. // self.title = Localized(@"JX_PublicNumber");
  57. // self.title = @"开奖";
  58. if(AppStore == 1){
  59. } else {
  60. _vcnum = (int)g_App.linkArray.count;
  61. }
  62. if (self.vcnum < 0 || self.vcnum >2) {
  63. self.vcnum = 0;
  64. }else{
  65. for (int i = 0; i < self.vcnum; i++) {
  66. if (i == 0) {
  67. _linkName1 = g_App.linkArray[i][@"desc"];
  68. _linkURL1 = g_App.linkArray[i][@"link"];
  69. _imgUrl1 = g_App.linkArray[i][@"imgUrl"];
  70. if (_linkName1.length < 1) {
  71. _linkName1 = @"百度";
  72. }
  73. if (_linkURL1.length < 1) {
  74. _linkURL1 = @"http://www.baidu.com";
  75. }
  76. }else if (i == 1){
  77. _linkName2 = g_App.linkArray[i][@"desc"];
  78. _linkURL2 = g_App.linkArray[i][@"link"];
  79. _imgUrl2 = g_App.linkArray[i][@"imgUrl"];
  80. if (_linkName2.length < 1) {
  81. _linkName2 = @"百度";
  82. }
  83. if (_linkURL2.length < 1) {
  84. _linkURL2 = @"http://www.baidu.com";
  85. }
  86. }
  87. }
  88. }
  89. self.wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self_width,self_height-JX_SCREEN_TOP)];
  90. self.wkWebView.backgroundColor=kRGBColor(250, 250, 250);
  91. [self.wkWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_linkURL1]]];
  92. self.wkWebView.UIDelegate = self;
  93. self.wkWebView.navigationDelegate = self;
  94. [self.view addSubview:self.wkWebView];
  95. }
  96. - (void)rightButtonClick {
  97. dispatch_async(dispatch_get_main_queue(), ^{
  98. [self.wkWebView reload];
  99. });
  100. }
  101. - (void)leftButtonClick {
  102. [self.wkWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.strURL]]];
  103. }
  104. // 根据WebView对于即将跳转的HTTP请求头信息和相关信息来决定是否跳转
  105. - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
  106. NSString * urlStr = navigationAction.request.URL.absoluteString;
  107. //NSLog(@"发送跳转请求:%@",urlStr);
  108. //自己定义的协议头
  109. decisionHandler(WKNavigationActionPolicyAllow);
  110. }
  111. // 根据客户端受到的服务器响应头以及response相关信息来决定是否可以跳转
  112. - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
  113. NSString * urlStr = navigationResponse.response.URL.absoluteString;
  114. NSLog(@"当前跳转地址:%@",urlStr);
  115. //允许跳转
  116. decisionHandler(WKNavigationResponsePolicyAllow);
  117. //不允许跳转
  118. //decisionHandler(WKNavigationResponsePolicyCancel);
  119. }
  120. // 页面是弹出窗口 _blank 处理
  121. - (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures {
  122. if (!navigationAction.targetFrame.isMainFrame) {
  123. [webView loadRequest:navigationAction.request];
  124. }
  125. return nil;
  126. }
  127. // 页面开始加载时调用
  128. - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
  129. [SVProgressHUD showWithStatus:@"正在加载中..."];
  130. }
  131. // 页面加载失败时调用
  132. - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
  133. [SVProgressHUD dismiss];
  134. }
  135. // 当内容开始返回时调用
  136. - (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
  137. [SVProgressHUD dismiss];
  138. }
  139. // 页面加载完成之后调用
  140. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
  141. [SVProgressHUD dismiss];
  142. }
  143. //提交发生错误时调用
  144. - (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
  145. [SVProgressHUD dismiss];
  146. }
  147. // 接收到服务器跳转请求即服务重定向时之后调用
  148. - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation {
  149. [SVProgressHUD dismiss];
  150. }
  151. - (void)viewWillAppear:(BOOL)animated{
  152. [super viewWillAppear:animated];
  153. [self.navigationController setNavigationBarHidden:YES animated:NO];
  154. }
  155. - (void)viewWillDisappear:(BOOL)animated{
  156. [super viewWillDisappear:animated];
  157. [self.navigationController setNavigationBarHidden:NO animated:NO];
  158. }
  159. @end