2011-01-26 6 views
2

그래,이 응용 프로그램에 네트워크 오류 경고 코드를 표시하려고합니다. SystemConfiguration.framework 프레임 워크와 Apple의 "Reachability"샘플 코드를 추가했습니다. 여기 iPhone 응용 프로그램에 네트워크 오류 경고를 표시하려고합니까?

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

      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 

앱에서 오류 또는 경고없이 잘 실행하지만 표시되지 않습니다 인터넷에 연결되어 있지 않으면 경고음이 울립니다. 왜이 사람이 어떤 생각을 가지고 있습니까?

+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

나는 실행 로그/콘솔에 액세스하는 법을 배웠고, 나는 그렇다고 생각한다. – Baccalad

관련 문제