码迷,mamicode.com
首页 > 移动开发 > 详细

ios 关于UIWebView控件的使用

时间:2016-05-06 16:18:19      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:


@interface XiaoCheDingWeiViewController ()<UIWebViewDelegate>
{
    JGProgressHUD *hud;
    UIView *_hudView;
    UIWebView *webView;
    NSString *IPString;
    NSString *string;
}
@end

@implementation XiaoCheDingWeiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //蓝色长条
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50) ];
    label.backgroundColor=[UIColor colorWithRed:46/255.0 green:104/255.0 blue:182/255.0 alpha:1.0];
    [self.view addSubview:label];
    UILabel *label0 = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2 - 50, 10, 100, 40)];
    label0.text = @"校车定位";
    label0.textColor = [UIColor whiteColor];
    label0.textAlignment = NSTextAlignmentCenter;
    label0.backgroundColor = [UIColor clearColor];
    [self.view addSubview:label0];
    self.view.backgroundColor = [UIColor whiteColor];
    //退出
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame =CGRectMake(10, 10, 100, 40);
    [backButton addTarget:self action:@selector(goToBack) forControlEvents:UIControlEventTouchUpInside];
    [backButton setTitle:@"< 返回" forState:UIControlStateNormal];
    backButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self.view addSubview:backButton];
    //清除缓存,通常的UIWebView默认是具有缓存功能的。
    [[NSURLCache sharedURLCache] removeAllCachedResponses];

    if (webView == nil) {
        webView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height-50) ];
    }
    webView.backgroundColor=[UIColor whiteColor];
    webView.scalesPageToFit=YES;
    webView.delegate = self;
    [self GetIP];
    if ([IPString isEqualToString:@"192.168.33"]) {
        
        string = @"http://192.168.33.203:8090/3dschool/car.htm";
    }else
    {
//        http://www.q-map.com.cn:8280
        string = @"http://kl.scst.edu.cn:8280/3dschool/car.htm";
//        string = @"http://www.q-map.com.cn:8280/3dschool/car.htm";
//        string = @"http://192.168.33.203:8090/3dschool/car.htm";
    }
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:string]]];
    [(UIScrollView *)[[webView subviews] objectAtIndex:0] setBounces:NO];
    [self.view addSubview:webView];
    [self showhudView];
    hud = [[JGProgressHUD alloc]initWithStyle:JGProgressHUDStyleDark];
    hud.textLabel.text = @"正在加载......";
    
}

#pragma mark - 获取当前网络ip地址
-(void)GetIP{
    NSString *String = [NSString deviceIPAdress];
    if ([String isEqualToString:@"an error occurred when obtaining ip address"]) {

      

    }else
    {
        NSArray *IPArray = [String componentsSeparatedByString:@"."];
        IPString = [NSString stringWithFormat:@"%@.%@.%@",IPArray[0],IPArray[1],IPArray[2]];
    }
}

- (void)showhudView
{
    _hudView = [[UIView alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height - 50)];
    _hudView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_hudView];
}

//在webView开始加载时会调用该函数,我们在这里显示coverView
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [hud showInView:_hudView animated:YES];
}

//在webView加载完毕时会调用该函数,我们在这里把coverView移除掉
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self RemoveHudView];
}

-(void)RemoveHudView
{
    
    [hud dismissAnimated:YES];
    if (_hudView) {
        [UIView animateWithDuration:0.5 animations:^{
            _hudView.alpha = 0;
        } completion:^(BOOL finished) {
            _hudView = nil;
        }];
    }

}


- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error
{
//    NSLog(@"%@",error);
    [self RemoveHudView];
    NSString *str = [NSString stringWithFormat:@"%@",error.localizedDescription]
    MBProgressHUD *_hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    
    // Set the annular determinate mode to show task progress.
    _hud.mode = MBProgressHUDModeText;
    _hud.label.text = NSLocalizedString(str, @"HUD message title");
    // Move to bottm center.
    
    _hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
    
    //    _hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
    
    [_hud hideAnimated:YES afterDelay:1.0];

    
}

- (void)goToBack{
       [self.navigationController popViewControllerAnimated:YES];
}

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

ios 关于UIWebView控件的使用

标签:

原文地址:http://blog.csdn.net/cxiao_11/article/details/51330392

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!