试图在我的应用程序中实现networking错误警报?

好的,我试图让这个应用程序显示networking错误警报代码。 我添加了SystemConfiguration.framework框架和Apple的“Reachability”示例代码。

这里是viewcontroller.h文件:

#import <UIKit/UIKit.h> @class Reachability; @interface Test_Internet_ConnectionViewController : UIViewController { Reachability* internetReachable; Reachability* hostReachable; } @property BOOL internetActive; @property BOOL hostActive; - (void) checkNetworkStatus:(NSNotification *)notice; @end 

这是viewcontroller.m文件:

 #import "Test_Internet_ConnectionViewController.h" #import "Reachability.h"; @implementation Test_Internet_ConnectionViewController @synthesize internetActive; @synthesize hostActive; /* // 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]; // check for internet connection [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil]; internetReachable = [[Reachability reachabilityForInternetConnection] retain]; [internetReachable startNotifier]; // check if a pathway to a random host exists hostReachable = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain]; [hostReachable startNotifier]; // now patiently wait for the notification } /* // 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. // eg self.myOutlet = nil; } - (void) checkNetworkStatus:(NSNotification *)notice { // called after network status changes NetworkStatus internetStatus = [internetReachable currentReachabilityStatus]; switch (internetStatus) { case NotReachable: { NSLog(@"The internet is down."); self.internetActive = NO; UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Internet Unavailable" message:@"This page cannot load, please check your internet connection and try again." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil]; [errorView show]; [errorView release]; break; } case ReachableViaWiFi: { NSLog(@"The internet is working via WIFI."); self.internetActive = YES; break; } case ReachableViaWWAN: { NSLog(@"The internet is working via WWAN."); self.internetActive = YES; break; } } NetworkStatus hostStatus = [hostReachable currentReachabilityStatus]; switch (hostStatus) { case NotReachable: { NSLog(@"A gateway to the host server is down."); self.hostActive = NO; break; } case ReachableViaWiFi: { NSLog(@"A gateway to the host server is working via WIFI."); self.hostActive = YES; break; } case ReachableViaWWAN: { NSLog(@"A gateway to the host server is working via WWAN."); self.hostActive = YES; break; } } } - (void)dealloc { [super dealloc]; [[NSNotificationCenter defaultCenter] removeObserver:self]; } @end 

如果打开应用程序时没有互联网,错误就会如预期那样工作,但是如果应用程序在通过互联网连接最初打开之后失去连接,错误将显示3次,当然我只想显示一次。

任何人都可以帮我吗? 提前致谢。

错误查看委托应该可能是自我而不是零,但我不知道这是否会解决问题。 如果没有,问题可能是NSNotificationCenter发送太多的通知给它的观察者(在这个例子中是checkNetworkStatus 。最简单的(但可能不太优雅的)解决scheme是保留一个通知计数器variables,当错误视图显示错误,如果计数器为零,则只显示错误视图。更好的解决scheme是检查传入的通知,并对不同的通知作出不同的响应(或根本不响应)。