2011-01-27 7 views
0

私はこのアプリにネットワークエラーアラートコードを表示させようとしています。私は、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. 
    // 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; 

      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回表示されます。明らかに1回だけ表示したいときです。

誰でも助けてくれますか? ありがとうございます。

答えて

1

View delegateエラーはおそらく自己であり、nilではないはずですが、問題が解決するかどうかはわかりません。表示されない場合は、NSNotificationCenterがオブザーバにあまりにも多くの通知を送信している可能性があります(この場合はcheckNetworkStatusです)。最も単純な(ただしあまりエレガントでない)ソリューションは、エラーが発生したときに増加する通知カウンタ変数を保持することですカウンタがゼロの場合にのみViewエラーを表示するより良い解決策は、着信通知をチェックし、異なる通知に別々に応答することです(まったく反応しません)。

+0

こんにちは、 – Baccalad

+0

Reachabilityクラスの中で通知を3回送信するかもしれないと思うのですが、私はこの問題を解決できませんでした。 、おそらく3つの異なる場所にあります。なぜそれがこれを行うのか知っていますか?とにかく私は解決策がth私はReachabilityクラスから2つの呼び出しを望んでいませんが、私はどのようにわかりません。これで助けてもらえますか? – Baccalad

+0

返信してください??? – Baccalad

関連する問題