// // PBLoadModelDisplayController.m // IphoneBIMe // // Created by zjf on 2018/7/24. // Copyright © 2018年 ProBIM. All rights reserved. // #import "PBLoadModelDisplayController.h" #import "PBAddIssueController.h" #import "PBNavigationController.h" #import #import "PBProjectModel.h" #import #import "PBBrowseViewController.h" @interface PBLoadModelDisplayController () @property (nonatomic, strong) WKWebView *webView; //@property (nonatomic, strong) JSContext *context; @end @implementation PBLoadModelDisplayController - (void)viewDidLoad { [super viewDidLoad]; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.f) { self.edgesForExtendedLayout = UIRectEdgeNone; self.navigationController.interactivePopGestureRecognizer.enabled = YES; self.navigationController.interactivePopGestureRecognizer.delegate = self; } [self setupUI]; } - (void)setupUI { self.title = @"三维浏览"; WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init]; configuration.userContentController = [WKUserContentController new]; [configuration.userContentController addScriptMessageHandler:self name:@"finshLoad"]; [configuration.userContentController addScriptMessageHandler:self name:@"base64Image"]; [configuration.userContentController addScriptMessageHandler:self name:@"preView"]; WKPreferences *preferences = [WKPreferences new]; preferences.javaScriptCanOpenWindowsAutomatically = YES; configuration.preferences = preferences; self.webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration]; self.webView.scrollView.bounces = NO; self.webView.navigationDelegate = self; self.webView.UIDelegate = self; [self.view addSubview:self.webView]; [self.webView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.left.right.bottom.equalTo(self.view); }]; PBBackNavItem *backNav = [PBBackNavItem backNacItem]; backNav.title = @"返回"; [backNav addTarget:self action:@selector(backNavAction) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *backNavItem = [[UIBarButtonItem alloc] initWithCustomView:backNav]; self.navigationItem.leftBarButtonItem = backNavItem; UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"新增问题" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)]; self.navigationItem.rightBarButtonItem = right; self.navigationItem.rightBarButtonItem.enabled = NO; [self loadWeb]; } - (void)loadWeb { NSString *url = [NSString stringWithFormat:@"%@/BIMComposer/index.html?projectId=%@&model=%@&ver=",WebUrl,self.projectIID, self.modelID]; NSLog(@"url:%@",url); [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]]; } - (void)backNavAction { [self.navigationController popViewControllerAnimated:YES]; } - (void)setProjectModel:(PBProjectModel *)projectModel { _projectModel = projectModel; } - (void)setModelID:(NSString *)modelID { _modelID = modelID; } - (void)setViewID:(NSString *)viewID { _viewID = viewID; } - (void)setViewPointID:(NSString *)viewPointID { _viewPointID = viewPointID; } - (void)setProjectIID:(NSString *)projectIID { _projectIID = projectIID; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } // 页面加载完成之后调用 - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation { NSLog(@"加载完成"); NSString *ocToJs = @"window.model.BIM365API.Events.finishRender.on('default',()=>{window.top.webkit.messageHandlers.finshLoad.postMessage(null)})"; [self.webView evaluateJavaScript:ocToJs completionHandler:^(id _Nullable name, NSError * _Nullable error) { NSLog(@"方法调用完成回调%@",error); }]; } - (void)rightAction { NSString *ocToJs = @"window.model.BIM365API.Context.asynCapturePicture().then((image_base64)=>{window.top.webkit.messageHandlers.base64Image.postMessage(image_base64)})"; [self.webView evaluateJavaScript:ocToJs completionHandler:^(id name, NSError * error) { NSLog(@"获取截图%@ - %@",name, error); }]; } #pragma mark - WKScriptMessageHandler - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message { // message.body -- Allowed types are NSNumber, NSString, NSDate, NSArray,NSDictionary, and NSNull. if ([message.name isEqualToString:@"finshLoad"]) { if (self.viewPointID) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ NSString *ocToJs = [NSString stringWithFormat:@"window.model.BIM365API.Context.setViewPointBasicInfo(%@)",self.viewPointID]; [self.webView evaluateJavaScript:ocToJs completionHandler:^(id _Nullable name, NSError * _Nullable error) { NSLog(@"相机位置设置完成"); }]; }); }else { self.navigationItem.rightBarButtonItem.enabled = YES; } }else if ([message.name isEqualToString:@"base64Image"]) { NSLog(@"图片回来了%@",message.body); // NSRange range = [message.body rangeOfString:@"base64,"]; // NSString *base64 = [message.body substringFromIndex:range.location + range.length]; // UIImage *image = [NSString imageDecoding:base64]; PBAddIssueController *addIssueVC = [[PBAddIssueController alloc] init]; addIssueVC.isAddIssue = YES; addIssueVC.projectModel = self.projectModel; // addIssueVC.statusAndTypeData = self.statusAndTypeData; addIssueVC.modelID = self.modelID; NSLog(@"获取相机位置"); NSString *ocToJs = @"window.model.BIM365API.Context.getViewPointBasicInfo(true)"; [self.webView evaluateJavaScript:ocToJs completionHandler:^(id name, NSError * error) { NSLog(@"方法调用完成回调%@ - %@",name, error); NSDictionary *dict = (NSDictionary *)name; NSString *str = [NSString convertToJson:dict]; addIssueVC.viewPointStr = str; }]; addIssueVC.base64Image = message.body; addIssueVC.SaveSuccessBlock = ^{ }; PBNavigationController *nav = [[PBNavigationController alloc] initWithRootViewController:addIssueVC]; nav.modalPresentationStyle = UIModalPresentationFullScreen; [self presentViewController:nav animated:YES completion:nil]; }else if ([message.name isEqualToString:@"preView"]) { NSLog(@"图片回来了%@",message.body); NSString *str = (NSString *)message.body; NSDictionary *dict = [NSString convertTodictionaryOrArr:str]; NSLog(@"%@",dict); PBBrowseViewController *browseVC = [[PBBrowseViewController alloc] init]; browseVC.details = dict; [self.navigationController pushViewController:browseVC animated:YES]; } } /* #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. } */ - (void)encodeWithCoder:(nonnull NSCoder *)coder { } - (void)traitCollectionDidChange:(nullable UITraitCollection *)previousTraitCollection { } - (void)preferredContentSizeDidChangeForChildContentContainer:(nonnull id)container { } - (void)systemLayoutFittingSizeDidChangeForChildContentContainer:(nonnull id)container { } - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(nonnull id)coordinator { } - (void)willTransitionToTraitCollection:(nonnull UITraitCollection *)newCollection withTransitionCoordinator:(nonnull id)coordinator { } - (void)didUpdateFocusInContext:(nonnull UIFocusUpdateContext *)context withAnimationCoordinator:(nonnull UIFocusAnimationCoordinator *)coordinator { } - (void)setNeedsFocusUpdate { } - (void)updateFocusIfNeeded { } @end