//
//  ErWeiMaViewController.m
//  YouXun
//
//  Created by laouhn on 15/12/22.
//  Copyright 漏 2015骞?Xboker. All rights reserved.
//

#import "ErWeiMaViewController.h"
#import "MBProgressHUD.h"


@interface ErWeiMaViewController ()<UIWebViewDelegate>
///浜岀淮鐮佸姞杞藉湪webview涓?@property (nonatomic, strong) UIWebView *myWeb;
@property (nonatomic, strong) MBProgressHUD *hud;

@end

@implementation ErWeiMaViewController

#warning 杩欓噷鏈変釜鍧戠偣.榛樿绠椾笂tabbar楂樺害,濡傛灉褰撳墠瑙嗗浘鎺у埗鍣ㄤ笉鏄剧ずtabbar鍒欓渶瑕侀珮搴︽坊鍔?9!
- (UIWebView *)myWeb {
    if (!_myWeb) {
        self.myWeb = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height )];
    }
    return _myWeb;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadTheString];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"杩斿洖" style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
}
- (void)back:(UIBarButtonItem *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)loadTheString {
    self.myWeb.delegate = self;
    self.hud = [MBProgressHUD showHUDAddedTo:self.myWeb animated:YES];
    self.hud.labelText = @"loading..";
    self.myWeb.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:self.myWeb];
    //    [self.myWeb scalesPageToFit];
    [self.myWeb setScalesPageToFit:YES];
    if ([self.myUrl hasPrefix:@"http://"] ) {
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.myUrl]];
        [self.myWeb loadRequest:request];
    }else {
        [self.myWeb loadHTMLString:[NSString stringWithFormat:@"<span style=\"font-size:40px;\"><span style=\"color:#000000;\">%@</span></span></span>", self.myUrl] baseURL:nil];
    }
    if ([[NSURL URLWithString:self.myUrl] checkResourceIsReachableAndReturnError:nil]) {
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.myUrl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
        [self.myWeb loadRequest:request];
    }
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [self.hud hide:YES];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    ///瓒呰繃10s娌℃湁鍝嶅簲,鎴栬€呰姹傚け璐?    [self requestFailed];
    [self.hud hide:YES];
}
///璇锋眰澶辫触
- (void)requestFailed {
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 30)];
    myView.backgroundColor = [UIColor darkGrayColor];
    myView.alpha = 0;
    UILabel *mylable = [[UILabel alloc] initWithFrame:CGRectMake(30, 5, [UIScreen mainScreen].bounds.size.width - 60, 20)];
    mylable.text = @"缃戠粶杩炴帴澶辫触,璇锋娴嬬綉缁滆缃?";
    mylable.textColor = [UIColor whiteColor];
    mylable.font = [UIFont systemFontOfSize:15];
    mylable.textAlignment = NSTextAlignmentCenter;
    [myView addSubview:mylable];
    [self.view addSubview:myView];
    [UIView animateWithDuration:1 animations:^{
        myView.alpha = 1;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1 animations:^{
            myView.alpha = 0;
        }];
    }];
}













- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #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
// 鐗堟潈灞炰簬鍘熶綔鑰?// http://code4app.com (cn) http://code4app.net (en)
// 鍙戝竷浠g爜浜庢渶涓撲笟鐨勬簮鐮佸垎浜綉绔? Code4App.com