在iPhone应用程序中显示没有互联网错误

问题描述:

再次您好,我试图在下面的应用程序中显示错误,如果没有互联网连接。这里是文件。 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 

很多人的使用由苹果公司提供的可达性例子: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

将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; 
} 

希望它能帮助。

如果您正在使用:

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error 

你可以在这里找到网络错误代码:

https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/Reference/reference.html#//apple_ref/doc/constant_group/URL_Loading_System_Error_Codes