2011-01-26 2 views
0

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

는 viewcontroller.h 파일입니다

#import <UIKit/UIKit.h> 

@class Reachability; 

@interface Test_Internet_ConnectionViewController : UIViewController { 

    Reachability* internetReachable; 
    Reachability* hostReachable; 
} 

- (void) checkNetworkStatus:(NSNotification *)notice; 

@end 

그리고 여기 viewcontroller.m 파일입니다

#import "Test_Internet_ConnectionViewController.h" 
#import "Reachability.h"; 

@implementation Test_Internet_ConnectionViewController 



/* 
// 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 

내가 6 오류를 얻을 : 멤버

요청 'internetActive' (3 번) 구조체 또는 공용체가 아닌 무언가에 'hostActive'멤버에 대한 요청 (3 번)

+0

더 많은 답변을 받아 들여야합니다. 9 %는 낮고 이상한 수용 비율입니다. 답변을 얻는 것은 좋지 않습니다. – Moshe

+0

조언 해 주셔서 감사합니다. 나는이 웹 사이트에 대해 아주 새로운 사람이다. 1 주 또는 2 주 새 것 같습니다. – Baccalad

+0

다른 질문을 게시하는 대신, 아무도 오류나 경고없이 앱이 실행되는 이유를 알 수 있지만 인터넷 연결이 없으면 오류 알림을 표시하지 않습니다. – Baccalad

답변

0

클래스 변수 또는 속성으로 'internetActive'및 'hostActive'를 만들고 있습니까? 나는 당신이 제공 한 코드에서 이것을 어디에도 보지 못했다.

+0

나는 생각하지 않는다, 나에게 이것을하는 법을 말해 줄 수 있습니까? – Baccalad

+0

그냥 @property (비 원자)를 추가하십시오. BOOL internetActive; 및 @property (비 원자) BOOL hostActive; 클래스의 헤더 파일에 추가 한 다음 구현 파일에서이를 합성합니다. 그런 다음 클래스의 모든 객체에서 해당 객체를 가져올 수 있습니다. – Matt

+0

정말 고맙습니다. 오류나 경고없이 실행되지만, 공항을 켤 때 오류 메시지가 표시되지 않습니까? – Baccalad

관련 문제