CustomWKWebViewController.m 5.86 KB
Newer Older
勾芒's avatar
勾芒 committed
1
//
2
//  CustomWKWebViewController.m
勾芒's avatar
勾芒 committed
3 4 5 6 7 8
//  Lighting
//
//  Created by 曹云霄 on 16/5/17.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

9
#import "CustomWKWebViewController.h"
10
#import <WebKit/WebKit.h>
曹云霄's avatar
曹云霄 committed
11
@interface CustomWKWebViewController ()<WKNavigationDelegate,WKScriptMessageHandler,UIScrollViewDelegate>
勾芒's avatar
勾芒 committed
12

13
@property (nonatomic,strong) WKWebView *webView;
14
@property (nonatomic,strong) WKWebViewConfiguration *config;
15
@property (nonatomic,strong) UIButton *dismissButton;
勾芒's avatar
勾芒 committed
16 17 18

@end

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
@implementation CustomWKWebViewController

#pragma mark - lazy
- (WKWebViewConfiguration *)config
{
    if (!_config) {
        _config = [[WKWebViewConfiguration alloc]init];
        // 设置偏好设置
        _config.preferences = [[WKPreferences alloc] init];
        // 默认为0
        _config.preferences.minimumFontSize = 10;
        // 默认认为YES
        _config.preferences.javaScriptEnabled = YES;
        // 在iOS上默认为NO,表示不能自动通过窗口打开
        _config.preferences.javaScriptCanOpenWindowsAutomatically = NO;
        // 通过JS与webview内容交互
        _config.userContentController = [[WKUserContentController alloc] init];
        [_config.userContentController addScriptMessageHandler:self name:@"AppModel"];
    }
    return _config;
}
勾芒's avatar
勾芒 committed
40

曹云霄's avatar
曹云霄 committed
41 42 43 44 45
- (WKWebView *)webView
{
    if (!_webView) {
        _webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:self.config];
        _webView.navigationDelegate = self;
46
        [self.view insertSubview:_webView atIndex:0];
曹云霄's avatar
曹云霄 committed
47 48 49 50 51
    }
    return _webView;
}


勾芒's avatar
勾芒 committed
52 53 54
- (void)viewDidLoad {
    [super viewDidLoad];
    
曹云霄's avatar
曹云霄 committed
55
    [self determineTheURLFileType];
曹云霄's avatar
曹云霄 committed
56
    [self addDismissButton];
勾芒's avatar
勾芒 committed
57 58
}

曹云霄's avatar
曹云霄 committed
59 60 61
#pragma mark - 判断加载文件类型
- (void)determineTheURLFileType
{
62
    [self loadURLfileAction];
曹云霄's avatar
曹云霄 committed
63 64 65 66
}

#pragma mark - 加载URL
- (void)loadURLfileAction
勾芒's avatar
勾芒 committed
67
{
68 69 70 71 72
    NSSet *websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];
    NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
    [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
        NSLog(@"清理缓存成功");
    }];
曹云霄's avatar
曹云霄 committed
73 74
}

75 76
#pragma mark - 数据
- (void)setUrlString:(NSString *)urlString
曹云霄's avatar
曹云霄 committed
77
{
78 79
    _urlString = urlString;
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_urlString]]];
曹云霄's avatar
曹云霄 committed
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
//#pragma mark - 加载PPT文件
//- (void)loadPPTfileAction
//{
//    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlString]]];
//    self.webView.scrollView.scrollEnabled = NO;
//    self.webView.scrollView.pagingEnabled = YES;
//    self.webView.scrollView.delegate = self;
//    self.countdownLabel.text = @"10";
//    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
//}
//
//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
//{
//    NSLog(@" end  %f",scrollView.contentOffset.y);
//    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
//    self.countdownLabel.text = @"10";
//    self.webView.scrollView.scrollEnabled = NO;
//}
//
//#pragma mark - 浏览PPT倒计时
//- (void)timerAction
//{
//    NSInteger number = [self.countdownLabel.text integerValue];
//    if (number <= 0) {
//        self.webView.scrollView.scrollEnabled = YES;
//        self.countdownLabel.text = @"请翻页";
//        [self.timer invalidate];
//        self.timer = nil;
//    }else{
//        number --;
//        self.countdownLabel.text = [NSString stringWithFormat:@"%ld",number];
//    }
//}
曹云霄's avatar
曹云霄 committed
115 116 117 118

#pragma mark - 添加删除按钮
- (void)addDismissButton
{
119 120 121
    self.dismissButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:self.dismissButton];
    [self.dismissButton mas_makeConstraints:^(MASConstraintMaker *make) {
122 123 124
        make.left.mas_equalTo(40);
        make.top.mas_equalTo(40);
        make.size.mas_equalTo(CGSizeMake(50, 50));
曹云霄's avatar
曹云霄 committed
125
    }];
126 127
    [self.dismissButton setBackgroundImage:TCImage(@"1") forState:UIControlStateNormal];
    [self.dismissButton addTarget:self action:@selector(dismissButtonClick) forControlEvents:UIControlEventTouchUpInside];
勾芒's avatar
勾芒 committed
128
}
勾芒's avatar
勾芒 committed
129

130 131 132 133 134 135
#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
    if ([message.name isEqualToString:@"AppModel"]) {
        // 打印所传过来的参数,只支持NSNumber, NSString, NSDate, NSArray,
        // NSDictionary, and NSNull类型
136 137 138 139 140 141 142 143 144 145 146 147 148
       id json = [NSJSONSerialization JSONObjectWithData:[message.body[@"body"] dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil];
        if (json) {
            if ([json isKindOfClass:[NSDictionary class]]) {
                NSDictionary *resultDict = (NSDictionary *)json;
                if ([[resultDict allKeys] containsObject:@"click"]) {
                    self.dismissButton.enabled = NO;
                }else {
                    self.dismissButton.enabled = YES;
                    if (self.luckyDrawFinishBlock) {
                        self.luckyDrawFinishBlock(resultDict);
                    }
                }
            }
149 150 151 152
        }
    }
}

勾芒's avatar
勾芒 committed
153
#pragma mark -dismiss
曹云霄's avatar
曹云霄 committed
154
- (void)dismissButtonClick
勾芒's avatar
勾芒 committed
155
{
156 157 158 159 160 161
    WS(weakSelf);
    [self dismissViewControllerAnimated:YES completion:^{
        if (weakSelf.dismissLuckyDrawController) {
            weakSelf.dismissLuckyDrawController();
        }
    }];
勾芒's avatar
勾芒 committed
162
}
勾芒's avatar
勾芒 committed
163

164
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation
勾芒's avatar
勾芒 committed
165
{
曹云霄's avatar
曹云霄 committed
166
    [XBLoadingView showHUDViewWithDefault];
勾芒's avatar
勾芒 committed
167
}
168 169

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
勾芒's avatar
勾芒 committed
170
{
曹云霄's avatar
曹云霄 committed
171
    [XBLoadingView hideHUDViewWithDefault];
勾芒's avatar
勾芒 committed
172
}
勾芒's avatar
勾芒 committed
173

174 175
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
曹云霄's avatar
曹云霄 committed
176
    [XBLoadingView showHUDViewWithText:@"加载失败"];
177 178
    [self dismissViewControllerAnimated:YES completion:nil];
}
勾芒's avatar
勾芒 committed
179 180 181 182




勾芒's avatar
勾芒 committed
183
@end