// // CYWebVeniceVC.m // shiku_im // // // // #import "CYWebAddPointVC.h" #import #import "UIView+LK.h" #import "JXShowMainView.h" #import "CYWebViewProgressView.h" #import "JXNodeView.h" @interface CYWebAddPointVC () @property (nonatomic,strong)WKWebView *wkWebView; @property (nonatomic,weak) JXNodeView *show; @end @implementation CYWebAddPointVC #pragma mark 接受新消息广播 - (UIStatusBarStyle)preferredStatusBarStyle{ return UIStatusBarStyleDefault; } //刷新网页 - (void)click:(UIButton *)sender{ [self.wkWebView reload]; } - (void)defineNavBarWeb:(NSString *)title andRinghtBtnImg:(NSString *)image { UIImageView *navView = [[UIImageView alloc] init]; navView.image=[UIImage imageNamed:@"goucaiNav"]; navView.frame = CGRectMake(0, 0, JX_SCREEN_WIDTH,44); [self.view addSubview:navView]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake((JX_SCREEN_WIDTH)/2-50,0, 100, 44)]; label.text = title; label.textColor = [UIColor whiteColor]; label.font = [UIFont systemFontOfSize:18 weight:UIFontWeightRegular]; label.textAlignment = NSTextAlignmentCenter; [navView addSubview:label]; UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom]; backBtn.frame = CGRectMake(JX_SCREEN_WIDTH-60,0,44, 44); [backBtn setTitle:image forState:0]; [backBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; [navView addSubview:backBtn]; } - (void)viewDidLoad { [super viewDidLoad]; [g_notify addObserver:self selector:@selector(rightButtonClick) name:KGoRefreshWebPageNotification object:nil]; [g_notify addObserver:self selector:@selector(leftButtonClick) name:KGoHomeWebPageNotification object:nil]; //以下代码适配大小 NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);"; WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES]; WKUserContentController *wkUController = [[WKUserContentController alloc] init]; [wkUController addUserScript:wkUScript]; //创建网页配置对象 WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init]; config.userContentController = wkUController; // 创建设置对象 config.preferences.minimumFontSize = 9.0; //设置请求的User-Agent信息中应用程序名称 iOS9后可用 // config.applicationNameForUserAgent = @"ChinaDailyForiPad"; self.wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self_width,self_height-JX_SCREEN_TOP) configuration:config]; config.preferences.minimumFontSize = 9.0; self.wkWebView.backgroundColor=kRGBColor(250, 250, 250); self.wkWebView.UIDelegate = self; self.wkWebView.navigationDelegate = self; _wkWebView.scrollView.showsVerticalScrollIndicator = NO; _wkWebView.scrollView.scrollsToTop = NO; // _wkWebView.scrollView.userInteractionEnabled = NO; if (@available(iOS 11.0,*)) { _wkWebView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } _wkWebView.scrollView.bounces = NO; CGRect frame = CGRectMake(0, 0, self.view.bounds.size.width, 2); CYWebViewProgressView *progressView = [[CYWebViewProgressView alloc] initWithFrame:frame]; [_wkWebView setProgressView:progressView]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[_strURL stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet]]] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20]; [self.wkWebView loadRequest:request]; [self.view addSubview:self.wkWebView]; NSString *memberAcc=[[NSUserDefaults standardUserDefaults] objectForKey:@"www_url"]; if (memberAcc.length>0) { }else{ [g_server actGetconfigconcatdomainnow:self]; } } //服务器返回数据 -(void) didServerResultSucces:(JXConnection*)aDownload dict:(NSDictionary*)dict array:(NSArray*)array1{ //更新本地好友 if ([aDownload.action isEqualToString:act_Getconfigconcatdomainnow]) { NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:dict[@"url"]] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20]; self.strURL = dict[@"url"]; [self.wkWebView loadRequest:request]; // [self.wkWebView reload]; } } -(int) didServerResultFailed:(JXConnection*)aDownload dict:(NSDictionary*)dict{ return hide_error; } -(int) didServerConnectError:(JXConnection*)aDownload error:(NSError *)error{//error为空时,代表超时 return hide_error; } //刷新 - (void)rightButtonClick { JXNodeView *show=[[JXNodeView alloc]init]; show.backgroundColor=[UIColor colorWithWhite:0.0 alpha:0.7]; show.frame=[UIScreen mainScreen].bounds; [[UIApplication sharedApplication].keyWindow addSubview:show]; __weak typeof(show) weakSelf=show; show.blockNode = ^(NSDictionary * _Nonnull dict) { [g_notify postNotificationName:@"changeNodeTitleNotifaction" object:dict[@"name"]]; [self.wkWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:dict[@"url"]]]]; [g_default setObject:dict[@"url"] forKey:@"selectNodeUrl"]; [g_default synchronize]; [self.wkWebView reload]; [weakSelf removeFromSuperview]; }; } - (void)leftButtonClick { [self.wkWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.strURL]]]; } // 根据WebView对于即将跳转的HTTP请求头信息和相关信息来决定是否跳转 - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { NSString * urlStr = navigationAction.request.URL.absoluteString; NSLog(@"发送跳转请求:%@",urlStr); //自己定义的协议头 decisionHandler(WKNavigationActionPolicyAllow); } // 根据客户端受到的服务器响应头以及response相关信息来决定是否可以跳转 - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{ NSString * urlStr = navigationResponse.response.URL.absoluteString; NSLog(@"当前跳转地址:%@",urlStr); //允许跳转 decisionHandler(WKNavigationResponsePolicyAllow); //不允许跳转 //decisionHandler(WKNavigationResponsePolicyCancel); } // 页面是弹出窗口 _blank 处理 - (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures { if (!navigationAction.targetFrame.isMainFrame) { [webView loadRequest:navigationAction.request]; } return nil; } // 页面开始加载时调用 - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation { // [SVProgressHUD showWithStatus:@"正在加载中..."]; } // 页面加载失败时调用 - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error { // [SVProgressHUD dismiss]; } // 当内容开始返回时调用 - (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation { // [SVProgressHUD dismiss]; } // 页面加载完成之后调用 - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation { // [SVProgressHUD dismiss]; } //提交发生错误时调用 - (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error { // [SVProgressHUD dismiss]; } // 接收到服务器跳转请求即服务重定向时之后调用 - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation { // [SVProgressHUD dismiss]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; } - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self.navigationController setNavigationBarHidden:YES animated:NO]; } - (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [self.navigationController setNavigationBarHidden:NO animated:NO]; } @end