CustomWKWebViewController.m 7.61 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>
11

12
@interface CustomWKWebViewController ()<WKNavigationDelegate,WKScriptMessageHandler,UIScrollViewDelegate,WKUIDelegate>
勾芒's avatar
勾芒 committed
13

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

@end

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
@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"];
38
        [_config.userContentController addScriptMessageHandler:self name:@"appShare"];
39 40 41
    }
    return _config;
}
勾芒's avatar
勾芒 committed
42

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

55
#pragma mark - 停止播放
56
-(void)viewDidDisappear:(BOOL)animated{
57
    [super viewDidDisappear:animated];
58 59 60
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
}

勾芒's avatar
勾芒 committed
61 62 63
- (void)viewDidLoad {
    [super viewDidLoad];
    
曹云霄's avatar
曹云霄 committed
64
    [self determineTheURLFileType];
曹云霄's avatar
曹云霄 committed
65
    [self addDismissButton];
勾芒's avatar
勾芒 committed
66 67
}

曹云霄's avatar
曹云霄 committed
68 69 70
#pragma mark - 判断加载文件类型
- (void)determineTheURLFileType
{
71
    [self loadURLfileAction];
曹云霄's avatar
曹云霄 committed
72 73 74 75
}

#pragma mark - 加载URL
- (void)loadURLfileAction
勾芒's avatar
勾芒 committed
76
{
77 78 79 80 81
    NSSet *websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];
    NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
    [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
        NSLog(@"清理缓存成功");
    }];
曹云霄's avatar
曹云霄 committed
82 83
}

84 85
#pragma mark - 数据
- (void)setUrlString:(NSString *)urlString
曹云霄's avatar
曹云霄 committed
86
{
87
    _urlString = urlString;
88 89 90 91 92
    if (self.type == Image) {
       [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:urlString]]];
    }else {
        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
    }
曹云霄's avatar
曹云霄 committed
93 94
}

95
#pragma mark - 添加关闭按钮
曹云霄's avatar
曹云霄 committed
96 97
- (void)addDismissButton
{
98 99 100
    self.dismissButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:self.dismissButton];
    [self.dismissButton mas_makeConstraints:^(MASConstraintMaker *make) {
101 102 103
        make.left.mas_equalTo(40);
        make.top.mas_equalTo(40);
        make.size.mas_equalTo(CGSizeMake(50, 50));
曹云霄's avatar
曹云霄 committed
104
    }];
105 106
    [self.dismissButton setBackgroundImage:TCImage(@"1") forState:UIControlStateNormal];
    [self.dismissButton addTarget:self action:@selector(dismissButtonClick) forControlEvents:UIControlEventTouchUpInside];
勾芒's avatar
勾芒 committed
107
}
勾芒's avatar
勾芒 committed
108

109 110 111
#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
112
    //抽奖结果
113 114 115
    if ([message.name isEqualToString:@"AppModel"]) {
        // 打印所传过来的参数,只支持NSNumber, NSString, NSDate, NSArray,
        // NSDictionary, and NSNull类型
116 117 118 119 120 121 122 123 124 125 126 127 128
       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);
                    }
                }
            }
129
        }
130 131 132 133
    }else if ([message.name isEqualToString:@"appShare"]) {
        //体验中心3D分享
        NSString *imgUrl = message.body[@"imgUrl"];
        NSString *panoUrl = message.body[@"panoUrl"];
134 135 136 137 138 139 140 141 142 143 144
        UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
        messageObject.text = @"欧立方一键分享";
        if ([[self class] isBlankString:panoUrl]) {
            UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
            [shareObject setShareImage:imgUrl];
            messageObject.shareObject = shareObject;
        }else {
            UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"欧立方体验中心3D分享" descr:nil thumImage:[UIImage imageNamed:@"shareIcon"]];
            shareObject.webpageUrl = panoUrl;
            messageObject.shareObject = shareObject;
        }
145
        [UMSocialGlobal shareInstance].isUsingHttpsWhenShareContent = NO;
146 147 148
        [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_WechatTimeLine messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
            if (error) {
                NSLog(@"************Share fail with error %@*********",error);
149
                ShowDefaultAlertView(nil, @"分享失败", UIAlertControllerStyleAlert, nil, nil);
150
            }else{
151 152 153
                [XBLoadingView showHUDViewWithSuccessText:@"分享微信朋友圈成功" completeBlock:nil];
            }
        }];
154 155 156
    }
}

157 158 159 160 161 162 163 164
-(WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
    if (!navigationAction.targetFrame.isMainFrame) {
        [webView loadRequest:navigationAction.request];
    }
    return nil;
}

勾芒's avatar
勾芒 committed
165
#pragma mark -dismiss
曹云霄's avatar
曹云霄 committed
166
- (void)dismissButtonClick
勾芒's avatar
勾芒 committed
167
{
168 169 170 171 172 173
    WS(weakSelf);
    [self dismissViewControllerAnimated:YES completion:^{
        if (weakSelf.dismissLuckyDrawController) {
            weakSelf.dismissLuckyDrawController();
        }
    }];
勾芒's avatar
勾芒 committed
174
}
勾芒's avatar
勾芒 committed
175

176
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation
勾芒's avatar
勾芒 committed
177
{
曹云霄's avatar
曹云霄 committed
178
    [XBLoadingView showHUDViewWithDefault];
勾芒's avatar
勾芒 committed
179
}
180 181

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
勾芒's avatar
勾芒 committed
182
{
曹云霄's avatar
曹云霄 committed
183
    [XBLoadingView hideHUDViewWithDefault];
勾芒's avatar
勾芒 committed
184
}
勾芒's avatar
勾芒 committed
185

186 187
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
188
    [XBLoadingView hideHUDViewWithDefault];
189
}
勾芒's avatar
勾芒 committed
190

191 192 193
#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
194
    
195 196 197
    CGFloat height = scrollView.frame.size.height;
    CGFloat contentYoffset = scrollView.contentOffset.y;
    CGFloat distanceFromBottom = scrollView.contentSize.height - contentYoffset;
198
    WS(weakSelf);
199
    if (distanceFromBottom <= height && self.type == Study) {
200
        ShowDefaultAlertView(nil, @"学习完成,是否开始考核?", UIAlertControllerStyleAlert, ^{
201 202
            if (weakSelf.scrollViewEndBottomBlock) {
                weakSelf.scrollViewEndBottomBlock(weakSelf.indexPath);
203
            }
204
        }, nil);
205 206 207
    }
}

勾芒's avatar
勾芒 committed
208 209 210



勾芒's avatar
勾芒 committed
211
@end