zjf
2023-03-06 392b76515f40376b6d36f40a114850ef63650384
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
//  PBBrowseViewController.m
//  IphoneBIMe
//
//  Created by zjf on 2021/3/1.
//  Copyright © 2021 ProBIM. All rights reserved.
//
 
#import "PBBrowseViewController.h"
#import <WebKit/WebKit.h>
 
@interface PBBrowseViewController ()<WKUIDelegate, WKNavigationDelegate>
@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