试图在iPhone应用程序上显示网络错误警报? [英] Trying to get network error alert to show on iPhone app?
问题描述
好的,所以我想让这个应用程序显示网络错误警报代码。我添加了SystemConfiguration.framework框架和Apple的Reachability示例代码。
Ok so I am trying to get this app to show network error alert codes. I have added the SystemConfiguration.framework framework and Apple's "Reachability" sample code.
这是viewcontroller.h文件:
Here is the viewcontroller.h file:
#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文件:
And here is the viewcontroller.m file:
#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.
// e.g. 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;
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
该应用程序运行良好,没有任何错误或警告,但没有互联网连接时,它不会显示警报。有没有人知道为什么会这样?
The app runs fine without any errors or warnings but it doesn't show an alert when there is no internet connection. Does anyone have any idea as to why this is?
推荐答案
你实际上并没有在任何地方显示警报。为此,您将执行类似以下操作:
You aren't actually displaying an alert anywhere. To do that, you'll do something similar to the following:
UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Internet Unavailable" message:@"MESSAGE ABOUT INTERNET UNAVAILABLE HERE." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[errorView show];
[errorView release];
此外,重要的是要注意viewDidLoad可以在视图控制器上被调用任意次数初始化。最好在指定的初始值设定项中声明任何通知观察者。
Also, it's important to note that viewDidLoad can be called an arbitrary number of times on a view controller after initialization. It's better to have any notification observers declared in the designated initializer instead.
这篇关于试图在iPhone应用程序上显示网络错误警报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!