|
@@ -0,0 +1,228 @@
|
|
|
+//
|
|
|
+// JYNewSearchViewController.m
|
|
|
+// JianYuIOS
|
|
|
+//
|
|
|
+// Created by apple on 2020/6/5.
|
|
|
+// Copyright © 2020 lixianglan. All rights reserved.
|
|
|
+//
|
|
|
+
|
|
|
+#import "JYNewSearchViewController.h"
|
|
|
+#import <WebKit/WebKit.h>
|
|
|
+#import "JYTabBarController.h"
|
|
|
+#import <CoreLocation/CoreLocation.h>
|
|
|
+
|
|
|
+@interface JYNewSearchViewController ()<WKNavigationDelegate,WKUIDelegate,WKScriptMessageHandler>
|
|
|
+
|
|
|
+@property (nonatomic, strong) UIProgressView *progressView;
|
|
|
+@property (nonatomic, strong) WKWebView *webView;
|
|
|
+@property (nonatomic, readonly) double estimatedProgress;//进度条
|
|
|
+
|
|
|
+@end
|
|
|
+
|
|
|
+@implementation JYNewSearchViewController
|
|
|
+
|
|
|
+-(void)viewWillAppear:(BOOL)animated
|
|
|
+{
|
|
|
+ [super viewWillAppear:animated];
|
|
|
+ [self.navigationController setNavigationBarHidden:YES animated:animated];
|
|
|
+}
|
|
|
+//进度条
|
|
|
+- (UIProgressView *)progressView
|
|
|
+{
|
|
|
+ if (_progressView == nil) {
|
|
|
+ _progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0,STATUS_BAR_HEIGHT - 2,WIDTH,2)];
|
|
|
+ _progressView.tintColor = [UIColor blueColor];
|
|
|
+ _progressView.trackTintColor = [UIColor whiteColor];
|
|
|
+ }
|
|
|
+ return _progressView;
|
|
|
+}
|
|
|
+
|
|
|
+- (WKWebView *)webView
|
|
|
+{
|
|
|
+ if(_webView == nil) {
|
|
|
+ WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
|
|
|
+ config.preferences = [[WKPreferences alloc] init];
|
|
|
+ config.preferences.minimumFontSize = 0;
|
|
|
+ config.preferences.javaScriptEnabled = YES;
|
|
|
+ config.preferences.javaScriptCanOpenWindowsAutomatically = NO;
|
|
|
+ //创建UserContentController(提供javaScript向webView发送消息的方法)
|
|
|
+ WKUserContentController *userContent = [[WKUserContentController alloc] init];
|
|
|
+ //添加消息处理,注意:self指代的是需要遵守WKScriptMessageHandler协议,结束时需要移除
|
|
|
+
|
|
|
+ [userContent addScriptMessageHandler:self name:@"loginByWeixin"];
|
|
|
+
|
|
|
+ //将UserContentController设置到配置文件中
|
|
|
+ config.userContentController = userContent;
|
|
|
+ //高端的自定义配置创建WKWebView
|
|
|
+ if (iPhoneX) {
|
|
|
+ _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-TAB_BAR_HEIGHT) configuration:config];
|
|
|
+ if (@available(iOS 11.0, *)) {
|
|
|
+ _webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, -STATUS_BAR_HEIGHT, WIDTH, HEIGHT-TAB_BAR_HEIGHT+STATUS_BAR_HEIGHT) configuration:config];
|
|
|
+ }
|
|
|
+ _webView.scrollView.bounces = NO;
|
|
|
+ _webView.UIDelegate = self;
|
|
|
+ _webView.navigationDelegate = self;
|
|
|
+ [_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
|
|
|
+ }
|
|
|
+ return _webView;
|
|
|
+}
|
|
|
+
|
|
|
+-(void)webViewFirstRequest
|
|
|
+{
|
|
|
+ NSString *url = [NSString stringWithFormat:@"%@%@", kHost, @"jyapp/free/index"];
|
|
|
+ NSString *token = [LXUserDefaults token];
|
|
|
+ if (token) {
|
|
|
+ url = [url stringByAppendingString:[NSString stringWithFormat:@"?sign=%@&url=/jyapp/jylab/mainSearch&from=restart", token]];
|
|
|
+ }
|
|
|
+ NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
|
|
|
+ [self.webView loadRequest:request];
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark - event response
|
|
|
+// 计算wkWebView进度条
|
|
|
+- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
|
|
|
+ if (object == self.webView && [keyPath isEqualToString:@"estimatedProgress"]) {
|
|
|
+ CGFloat newprogress = [[change objectForKey:NSKeyValueChangeNewKey] doubleValue];
|
|
|
+ self.progressView.alpha = 1.0f;
|
|
|
+ [self.progressView setProgress:newprogress animated:YES];
|
|
|
+ if (newprogress >= 1.0f) {
|
|
|
+ [UIView animateWithDuration:0.3f
|
|
|
+ delay:0.3f
|
|
|
+ options:UIViewAnimationOptionCurveEaseOut
|
|
|
+ animations:^{
|
|
|
+ self.progressView.alpha = 0.0f;
|
|
|
+ }
|
|
|
+ completion:^(BOOL finished) {
|
|
|
+ [self.progressView setProgress:0 animated:NO];
|
|
|
+ }];
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+- (void)viewDidLoad {
|
|
|
+ [super viewDidLoad];
|
|
|
+ // Do any additional setup after loading the view.
|
|
|
+ NSLog(@"首页初始化");
|
|
|
+ self.view.backgroundColor = [UIColor whiteColor];
|
|
|
+ [self.view addSubview:self.webView];
|
|
|
+ [self.view addSubview:self.progressView];
|
|
|
+ [self webViewFirstRequest];//加载服务器url的方法
|
|
|
+}
|
|
|
+#pragma mark - WKNavigationDelegate
|
|
|
+/* 页面加载完成 */
|
|
|
+- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
|
|
|
+ NSLog(@"-----页面加载完成");
|
|
|
+}
|
|
|
+/* 页面开始加载 */
|
|
|
+- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
|
|
|
+
|
|
|
+}
|
|
|
+/* 开始返回内容 */
|
|
|
+- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
|
|
|
+
|
|
|
+}
|
|
|
+/* 页面加载失败 */
|
|
|
+- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+#pragma mark *****JS与OC交互
|
|
|
+#pragma mark - WKScriptMessageHandler JS调OC交互
|
|
|
+- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
|
|
|
+ NSLog(@"原生收到了js发送过来的消息message.name = %@ message.body = %@",message.name,message.body);
|
|
|
+ if ([message.name isEqualToString:@"loginByWeixin"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"share"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"saveUserToken"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"getUserToken"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"removeUserToken"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"checkNoticePermission"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"openSystemNotification"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"getPushRid"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"lightStatusBar"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"openExternalLink"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"getVersion"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"alert"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"isInstallWeixin"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"changeMessageType"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"openActivityPage"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"getCipherText"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"business"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"hiddenBottom"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"checkLab"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"loginSuccess"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"backUrl"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"clearPushMessage"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"hideRedSpotOnMenu"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"showRedSpotOnMenu"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"wxPay"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"aliPay"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"getOtherPushRid"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"getPhoneBrand"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"getLocation"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"chooseTab"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"skipCamera"]) {
|
|
|
+
|
|
|
+ }else if ([message.name isEqualToString:@"skipAlbum"]) {
|
|
|
+
|
|
|
+ }else {
|
|
|
+
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark - OC调JS交互
|
|
|
+-(void)ocCallToJS:(NSString *)jsStr{
|
|
|
+ [self.webView evaluateJavaScript:jsStr completionHandler:^(id _Nullable result, NSError * _Nullable error) {
|
|
|
+ NSLog(@"%@----%@",result, error);
|
|
|
+ }];
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+/*
|
|
|
+#pragma mark - Navigation
|
|
|
+
|
|
|
+// In a storyboard-based application, you will often want to do a little preparation before navigation
|
|
|
+- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
|
|
|
+ // Get the new view controller using [segue destinationViewController].
|
|
|
+ // Pass the selected object to the new view controller.
|
|
|
+}
|
|
|
+*/
|
|
|
+
|
|
|
+@end
|