2011-01-26 9 views
2

私はこのアプリにネットワークエラーアラートコードを表示させようとしています。私は、SystemConfiguration.frameworkフレームワークとAppleの "Reachability"サンプルコードを追加しました。iPhoneアプリにネットワークエラーアラートを表示しようとしていますか?

#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; 

      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 

アプリはエラーや警告なしで細かい実行されますが、それは表示されません。ここ

はviewcontroller.hファイルですインターネットに接続していないときに警告を発する。なぜこれがあるのか​​誰にも分かりますか?

+0

誰でもお手伝いできますか? – Baccalad

+0

してください私は本当にこれを動作させる必要があります。 – Baccalad

+0

投稿したコードにはアラートが表示されず、一部のメッセージのみが記録されます。実行ログにメッセージが表示されていますか? –

答えて

3

実際にはどこにアラートを表示していません。

UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Internet Unavailable" message:@"MESSAGE ABOUT INTERNET UNAVAILABLE HERE." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil]; 
[errorView show]; 
[errorView release]; 

また、それはのviewDidLoadが初期化後のビューコントローラ上の任意の回数呼び出すことができることに注意することが重要です。これを行うには、次のような何かをするだろう。指定されたイニシャライザで通知オブザーバが宣言されている方が良いでしょう。

+0

ご協力いただきありがとうございます。どうすればいいですか? – Baccalad

+0

少なくとも実行中のプログラムのコンソールにNSLogステートメントが表示されているはずです。あなたですか? Xcodeから実行ログにアクセスするには、実行>コンソールを選択します。 –

+0

私はrun log/consoleのものにアクセスする方法を工夫しました。私はそうだと思います。 – Baccalad