ios 关于UIWebView控件的使用


@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];
   
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值