// // PBBrowseViewController.m // IphoneBIMe // // Created by zjf on 2021/3/1. // Copyright © 2021 ProBIM. All rights reserved. // #import "PBBrowseViewController.h" #import @interface PBBrowseViewController () @property (nonatomic, strong) WKWebView *webView; @end @implementation PBBrowseViewController - (void)viewDidLoad { [super viewDidLoad]; [self setupUI]; } - (void)setupUI { self.view.backgroundColor = [UIColor whiteColor]; self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds]; 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; [self loadWeb]; } - (void)loadWeb { long type = [[self.details valueForKey:@"Type"] longValue]; NSString *content = [self.details valueForKey:@"Content"]; //1图片 2文件 3链接 if (type == 1 || type == 2) { NSRange range = [content rangeOfString:@"base64,"]; NSString *base64 = [content substringFromIndex:range.location + range.length]; NSData *data = [[NSData alloc]initWithBase64EncodedString:base64 options:NSDataBase64DecodingIgnoreUnknownCharacters]; NSString *fileName = [self.details valueForKey:@"Name"]; NSRange range1 = [fileName rangeOfString:@"|"]; NSString *name = [fileName substringFromIndex:range1.location + range1.length]; NSString *path_document = NSTemporaryDirectory(); NSString *imagePath = [path_document stringByAppendingString:name]; NSFileManager *fileManger = [NSFileManager defaultManager]; [fileManger createFileAtPath:imagePath contents:data attributes:nil]; [self.webView loadFileURL:[NSURL fileURLWithPath:imagePath] allowingReadAccessToURL:[NSURL fileURLWithPath:imagePath]]; // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // }); }else if (type == 3) { // if ([content hasPrefix:@"https"] || [content hasPrefix:@"http"]) { // } [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:content]]]; } } - (void)backNavAction { long type = [[self.details valueForKey:@"Type"] longValue]; if (type != 3) { NSString *fileName = [self.details valueForKey:@"Name"]; NSRange range1 = [fileName rangeOfString:@"|"]; NSString *name = [fileName substringFromIndex:range1.location + range1.length]; NSString *path_document = NSTemporaryDirectory(); NSString *imagePath = [path_document stringByAppendingString:name]; NSFileManager *fileManger = [NSFileManager defaultManager]; [fileManger removeItemAtPath:imagePath error:nil]; } [self.navigationController popViewControllerAnimated:YES]; } - (void)setDetails:(NSDictionary *)details { _details = details; } #pragma mark -- 实现WKNavigationDelegate委托协议 //开始加载时调用 -(void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation{ NSLog(@"开始加载"); } //当内容开始返回时调用 -(void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation{ NSLog(@"内容开始返回"); } //加载完成之后调用 -(void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{ NSLog(@"加载完成"); } //加载失败时调用 -(void)webView:(WKWebView *)webView didFailLoadWithError:(nonnull NSError *)error{ NSLog(@"加载失败 error : %@",error.description); } /* #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