你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> ios 關於UIWebView控件的使用

ios 關於UIWebView控件的使用

編輯:IOS開發綜合

@interface XiaoCheDingWeiViewController ()
{
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. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved