WebViewJavascriptBridge JS和OC互動

weixin_34124651發表於2018-05-04
#import "CBFeedbackController.h"
#import "WebViewJavascriptBridge.h"
#import <WebKit/WebKit.h>

@interface CBFeedbackController ()<UIWebViewDelegate>
@property (nonatomic, strong) WKWebView *htmlWebView;
@property (nonatomic, strong) WebViewJavascriptBridge *bridge;
@property (nonatomic, strong) UIProgressView *progressView;
@end

@implementation CBFeedbackController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationBarWhiteColor];
}

- (void)createUI {
    self.title = @"意見反饋";
    [self.view addSubview:self.htmlWebView];
    [self.htmlWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)updateView {
    kWeakSelf(self)
    [self.htmlWebView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(weakself.view);
    }];
}

- (void)createViewModel {
    kWeakSelf(self)
    self.bridge = [WebViewJavascriptBridge bridgeForWebView:self.htmlWebView];
    
    NSString *userNo = [CBDataAccessData getUserData].userNo;
    NSString *token = [CBDataAccessData getUserData].token;
    NSDictionary *dict = @{@"userNo":userNo, @"token":token, @"sysfrom": @"ios" };
    [self.bridge callHandler:@"getUserMessage" data:dict responseCallback:^(id responseData) { }];
    
    [self.bridge registerHandler:@"getStatus" handler:^(id data, WVJBResponseCallback responseCallback) {
        NSString *message = [NSString stringWithFormat:@"%@",data];
        [CBTipBoxView showTopTipSuccessfulContent:message];
        [weakself.navigationController popViewControllerAnimated:YES];
    }];
}

#pragma mark - KVC and KVO
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([@"estimatedProgress" isEqualToString:keyPath]) {
        [self.progressView setProgress:self.htmlWebView.estimatedProgress animated:NO];
    }
    
    if (self.progressView.progress == 0) {
        self.progressView.hidden = NO;
    } else if (self.progressView.progress == 1) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if (self.progressView.progress ==1) {
                self.progressView.progress = 0;
                self.progressView.hidden = YES;
            }
        });
    }
}

#pragma mark --- 例項化
- (WKWebView *)htmlWebView {
    if (_htmlWebView == nil) {
        _htmlWebView = [[WKWebView alloc] init];
        NSURLRequest *requestUrl = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://baidu.com"]];
        [_htmlWebView loadRequest:requestUrl];
    }
    
    return _htmlWebView;
}

-(UIProgressView *)progressView {
    if (_progressView == nil) {
        CGRect  rect = CGRectZero;
        rect.size.width = [[UIScreen mainScreen]bounds].size.width;
        rect.size.height = 2;
        _progressView = [[UIProgressView alloc]initWithFrame:rect];
        _progressView.progressTintColor = [UIColor hexColor:@"#FFC400"];
        [_progressView setProgressViewStyle:UIProgressViewStyleDefault];
        [self.view addSubview:_progressView];
    }
    return _progressView;
}

- (void)dealloc {
    [self.htmlWebView removeObserver:self forKeyPath:@"estimatedProgress"];
    self.htmlWebView = nil;
    [self.htmlWebView removeFromSuperview];
}

@end

相關文章