2011-05-10 104 views
0

再次您好,我試圖在下面的應用程序中顯示錯誤,如果沒有互聯網連接。這裏是文件。 FirstViewController.h:在iPhone應用程序中顯示沒有互聯網錯誤

// 
// FirstViewController.h 
// DailyJoke 
// 
// Created by John Bridge on 5/4/11. 
// Copyright 2011 Bridge and co. All rights reserved. 
// 

#import <UIKit/UIKit.h> 


@interface FirstViewController : UIViewController { 
    IBOutlet UIWebView *webView; 
} 
@end 

這裏是FirstViewController.m: // // FirstViewController.m // DailyJoke // // 創建者約翰·橋11年5月4日。 //版權所有2011 Bridge and co。版權所有。 //

#import "FirstViewController.h" 


@implementation FirstViewController 




-(void)awakeFromNib 
{ 
     [webView loadRequest:[NSURLRequest requestWithURL:[NSURL    URLWithString:@"http://www.johnbridge180.com/IPhoneIPad/IPhone/DailyJoke/DailyJoke.html"]]]; 
} 

/* 
// The designated initializer. Override to perform setup that is required before the  view is loaded. 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 
*/ 

/* 
// Implement loadView to create a view hierarchy programmatically, without using a nib. 
- (void)loadView { 
} 
*/ 

/* 
// Implement viewDidLoad to do additional setup after loading the view, typically from a  nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
} 
*/ 

/* 
// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
*/ 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


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

@end 

回答

1

很多人的使用由蘋果公司提供的可達性例子:http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html

看起來像: +(可達性*)reachabilityWithHostName:(的NSString *)主機名; 將是地方的開始。

另一種方法是使用NSURLConnection加載HTML並將其提供給webview。如果URL連接失敗,客戶端可能沒有互聯網連接。

+0

我仍然困惑srry即時通訊新 – 2011-05-10 01:36:13

+0

你應該花時間去嘗試的示例代碼或者在放棄之前閱讀更多文檔。 – pzearfoss 2011-05-10 01:37:54

2

將UIWebViewDelegate實現到您的控制器類。並覆蓋控制器類中的以下方法。這些方法名稱是自解釋的。你需要相應地處理錯誤。通過在viewDidLoad方法中設置以下句子,將您的webview委託設置爲您的控制器類。也不要忘記爲您的類實現UIWebViewDelegate。

  • webView.delegate = self;
#pragma mark webView delegate 
-(void)webViewDidStartLoad:(UIWebView *)wView{ 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = TRUE; 
} 
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{ 
    if ([error code] == -999) { 
    } 
    else if([error code] == -1009 || [[error localizedDescription] isEqualToString:@"no Internet connection"]){ 
     [UIApplication sharedApplication].networkActivityIndicatorVisible = FALSE; 
    } 
    else if([error code] == -1001 || [[error localizedDescription] isEqualToString:@"timed out"]){  
    } 
    else if([error code] == -1004 || [[error localizedDescription] isEqualToString:@"can’t connect to host"]){    
    } 
    else if (error != NULL) {  
    } 
    else{  
    } 
} 
-(void)webViewDidFinishLoad:(UIWebView *)wView{ 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = FALSE; 
} 

希望它能幫助。

相關問題