陈斌彬的技术博客

Stay foolish,stay hungry

Webview初始化

//
//  BBPeiZhenFuWuVC.m
//  HangyuHealth
//
//  Created by lwj on 16/8/23.
//  Copyright © 2016年 AntWork. All rights reserved.
//

#import "BBPeiZhenFuWuVC.h"

@interface BBPeiZhenFuWuVC () <UITableViewDataSource, UITableViewDelegate,UIWebViewDelegate>
{
    UITableView *_mTableView;
    UIBarButtonItem *_rightItem;
    BOOL bFirstLoad;
}

@property (strong, nonatomic) UIWebView *webView;
@property WebViewJavascriptBridge* bridge;

@end

@implementation BBPeiZhenFuWuVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"陪诊服务";
    [self initTableView];
    [self initUI];

}

- (void) initUI {

    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [rightButton setTitle:@"提交" forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [rightButton setTitleColor:[UIColor colorWithRed:32 / 255.0 green:134 / 255.0 blue:158 / 255.0 alpha:1.0] forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [rightButton addTarget:self action:@selector(addBind) forControlEvents:UIControlEventTouchUpInside];
    _rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}

- (void)addBind {
    [_bridge callHandler:@"save" data:nil];
}

- (void)returnToPreviosView {
    if(_webView.canGoBack){
        [_webView goBack];
    } else{
        [super returnToPreviosView];
    }
}

#pragma mark - UITableViewDelegate

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier = [NSString stringWithFormat:@"%@Identify", NSStringFromClass([self class])];
    UITableViewCell *returnCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(returnCell == nil)
    {
        returnCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }

    //清除UIWebView的缓存
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    _webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:_webView];

    [WebViewJavascriptBridge enableLogging];
    _bridge = [WebViewJavascriptBridge bridgeForWebView:_webView];
    [_bridge setWebViewDelegate:self];
    [_bridge registerHandler:@"finishLoading" handler:^(id data, WVJBResponseCallback responseCallback) {

        [MBProgressHUD hideHUDForView:self.view animated:YES];

    }];
//    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];
    UserDataCenter *udc = [UserDataCenter sharedInstance];
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://yikangbao.yucanlin.cn:9081/registration/views/query-inventory.html?access_token=%@", udc.userInfo.strAccessToken]]]];
    [returnCell.contentView addSubview:_webView];
    returnCell.selectionStyle = UITableViewCellSelectionStyleNone;
    return returnCell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSInteger num = 1;
    return num;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return _mTableView.frame.size.height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    CGFloat height = 0.01;
    return height;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view=[[UIView alloc] init];
    view.backgroundColor = color_ffffff;
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    CGFloat height = 0.01;
    return height;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view=[[UIView alloc] init];
    view.backgroundColor = color_ffffff;
    return view;
}


- (void)initTableView {
    _mTableView = [[TPKeyboardAvoidingTableView alloc] initWithFrame:(CGRect) {0, 0, screenWidth, self.mViewContentHeight}];
    _mTableView.backgroundColor = color_f7f7f9;
    _mTableView.delegate = self;
    _mTableView.dataSource = self;
    _mTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [_mTableView reloadData];
    [self.view addSubview:_mTableView];
}

- (void)webViewDidStartLoad:(UIWebView *)webView {

    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {

    [MBProgressHUD hideHUDForView:self.view animated:YES];
    [_bridge callHandler:@"reload" data:nil];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error {
    [MBProgressHUD showHUD:self text:[error localizedDescription]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end