2011-01-23 9 views
1

新しいiOS 4.2プロジェクトでAppleのReachability 2.2クラスを実装しました。デバイスがネットワーク接続を失ったときに警告表示を表示するだけです。 (それ以降、App Storeのインターフェイスのユーザビリティ要件に準拠しています。)this previous SO questionを私の出発点として使用しました。アプリケーションが正しく(接続が中断または復元されたとき)正しく通知しているようですが、私はNS Alertビューでループします。私のエラーは幾分基本的でなければならないと思うが、私はそれをキャッチできない。 NS AlertViewなしでこれを行うためのよりクリーンな方法がある場合、私はそれにもオープンしています。私は以下のコードからいくつかのメソッドを残しましたが、アプリケーションはかなり簡単で、ViewControllerは1つしかありません。NSAlertsとNSNotificationsをReachability 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]; 

} 

答えて

1

通知が送信され、処理された後、その通知は、通知センターから削除する必要があることを忘れないでください。それ以外の場合は、到達可能性のステータスが変化するたびにアラートが表示されます。例えば

、の通知を削除し、あなたが行うことができます:

[[NSNotificationCenter defaultCenter] removeObserver:self 
name:kReachabilityChangedNotification 
object:nil]; 

は、あなたがする必要がある場合、後で再度追加することを忘れないでください。

+0

ありがとうございました。私は[[NSNotificationCenter defaultCenter] removeObserver:self]を持っています。私の - (void)viewDidUnload ...それは間違った場所にありますか? – mozzer

0

通知ビューを追加すると、応答を処理するのに十分です。さらに時間を追加すると、コールバックによって何度も呼び出されます。

関連する問題