我已經在新的iOS 4.2項目中實現了Apple的Reachability 2.2類。我只想在設備失去網絡連接時顯示警報視圖。 (因此符合App Store的界面可用性要求。)我用this previous SO question作爲我的出發點。該應用程序似乎正確通知(當連接中斷或恢復),但我得到循環與我的NS警報視圖。我認爲我的錯誤一定是基本的,但我無法理解。如果有沒有NS AlertView的更清潔的方法,我也可以這樣做。我在下面的代碼中留下了一些方法,但該應用程序非常簡單,只有一個ViewController。使用NSAlerts和NSNotifications和可達性2.2
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
ViewController.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];
}
謝謝。我有[[NSNotificationCenter defaultCenter] removeObserver:self];在我的 - (無效)viewDidUnload ...是否在錯誤的位置? – mozzer 2011-01-23 23:11:41