在載入網頁時給一個timer定時器,規定超時時間,然後再超時時間的方法中提示超時
如果沒有超時,則在webview協議中的“載入完成”方法中 取消timer定時器
- - (void)openWebView
- {
- if (timer) {
- [timer invalidate];
- }
- timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(timerFunction) userInfo:nil repeats:NO];
- if (!webViewFirst) {
- webViewFirst = [[UIWebView alloc] init];
- webViewFirst.frame = CGRectMake(, , ivWelcome.frame.size.width, ivWelcome.frame.size.height);
- webViewFirst.backgroundColor = [UIColor whiteColor];
- webViewFirst.delegate = self;
- webViewFirst.hidden = YES;
- [webViewFirst loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[kGlobal getLoadingUrl]]]];
- [ivWelcome addSubview:webViewFirst];
- UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
- btn.frame = CGRectMake(, , ivWelcome.frame.size.width, ivWelcome.frame.size.height);
- btn.backgroundColor = [UIColor clearColor];
- btn.enabled = NO;
- [btn addTarget:self action:@selector(initLoginView) forControlEvents:UIControlEventTouchUpInside];
- [ivWelcome addSubview:btn];
- btnSkip = btn;
- }
- else {
- webViewFirst.hidden = YES;
- [webViewFirst reload];
- }
- }
- - (void)webViewDidFinishLoad:(UIWebView *)webView
- {
- if (timer) {
- [timer invalidate];
- }
- btnSkip.enabled = YES;
- webViewFirst.hidden = NO;
- {//新增淡入淡出動畫
- CATransition *animation = [CATransition animation];
- //animation.delegate = self;
- // 設定動畫時間
- animation.duration = 0.7;
- // 設定動畫快慢(開始與結束時較慢)
- animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
- animation.type = kCATransitionFade;
- [webViewFirst.layer addAnimation:animation forKey:@"animation"];
- }
- }
- - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
- {
- [self timerFunction];
- }
- - (void)timerFunction
- {
- if (timer) {
- [timer invalidate];
- }
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"網路不給力,請重試" delegate:kAPPDELEGATE cancelButtonTitle:@"重試" otherButtonTitles:nil];
- [alert show];
- }