我在一个新的 iOS 4.2 项目中实现了 Apple 的 Reachability 2.2 类。我只想在设备失去网络连接时出现警报视图。(因此符合 App Store 的界面可用性要求。)我使用这个之前的 SO 问题作为我的起点。该应用程序似乎可以正确通知(当连接中断或恢复时),但我的 NS 警报视图正在循环。我认为我的错误一定是基本的,但我无法抓住它。如果在没有 NS AlertView 的情况下有更简洁的方法来做到这一点,我也对此持开放态度。我在下面的代码中留下了一些方法,但该应用程序非常简单,只有一个 ViewController。
视图控制器.h:
#import <UIKit/UIKit.h>
#import "Reachability.h"
@class Reachability;
@interface ViewController : UIViewController {
IBOutlet UITextView *liveOutputTextView;
IBOutlet UITextView *textView;
Reachability* internetReachable;
Reachability* hostReachable;
}
-(IBAction)action1:(id)sender;
-(IBAction)action2:(id)sender;
-(void)textFieldDidUpdate:(id)sender;
-(void)checkNetworkStatus:(NSNotification *)notice;
@end
视图控制器.m
#import "ViewController.h"
#import "Reachability.h"
@implementation ViewController
@synthesize liveOutputTextField;
- (void)checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
case NotReachable:
{
NSLog(@"The internet is inaccessible.");
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Internet inaccessible."
message:@"Internet inaccessible."
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
break;
}
case ReachableViaWiFi:
{
NSLog(@"Internet Connetion is UP.");
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Internet is Up"
message:@"Internet Up"
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
break;
}
case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN.");
break;
}
}
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
NSLog(@"A gateway to the host server is down.");
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Host is Down"
message:@"Host Down"
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
break;
}
case ReachableViaWiFi:
{
NSLog(@"A gateway to the host server is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
NSLog(@"A gateway to the host server is working via WWAN.");
break;
}
}
}
- (void)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
}
- (void)viewDidAppear:(BOOL)animated {
// 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
}
-(IBAction) action1:(id)sender {
// 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
// ** LEFT OUT ACTUAL CODE FOR BREVITY **
}
-(IBAction) action2:(id)sender {
// 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
// ** LEFT OUT ACTUAL CODE FOR BREVITY **
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)dealloc {
[super dealloc];
}