2012-08-23 4 views
0

Xcode 4.4의 연결 가능성을 사용하여 사용자가 인터넷에 연결되어 있지 않은 경우 사용자에게 알리려고합니다. 내 초기보기 컨트롤러에는 온라인 테이블에서 채워지는 테이블을로드하는 버튼이 있습니다. 스택 오버플로에서 몇 가지 예제를 따랐지만 작동하지 못했습니다. 내가 버튼을 사용하고 있기 때문에reachability를 사용하여 인터넷 연결에 대해 사용자에게 알립니다.

#import "MainPageViewController.h" 
#import "Reachability.h" 

@implementation MainPageViewController 
@synthesize internetActive; 
@synthesize hostActive; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
    // Custom initialization 
    } 
return self; 
} 

- (void) viewWillAppear:(BOOL)animated 
    { 
    [[NSNotificationCenter defaultCenter] addObserver: self selector: 
    @selector(checkNetworkStatus:) name: kReachabilityChangedNotification object: nil]; 

    internetReachable = [Reachability reachabilityForInternetConnection]; 
    [internetReachable startNotifier]; 

    hostReachable = [Reachability reachabilityWithHostname: @"sites.google.com"]; 
    [hostReachable startNotifier]; 
    } 

- (void) checkNetworkStatus:(NSNotification *)notice 
    { 
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus]; 
    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus]; 

    if((internetStatus == NotReachable) && (hostStatus == NotReachable)) 
    { 
     UIAlertView *internetAlert = [[UIAlertView alloc] initWithTitle:@"Network Error!" 
     message: @"You are not connected to the internet!" delegate: self 
     cancelButtonTitle: @"Ok" otherButtonTitles: nil]; 
     [internetAlert show]; 
     self.internetActive = NO; 
     self.hostActive = NO; 
    } 
    } 

-(void) dealloc 
    { 
    [[NSNotificationCenter defaultCenter] removeObserver: self]; 
    } 

- (void)viewDidLoad 
    { 
    [super viewDidLoad]; 
// Do any additional setup after loading the view. 
    } 

- (void)viewDidUnload 
    { 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    } 

- (BOOL)shouldAutorotateToInterfaceOrientation(UIInterfaceOrientation)interfaceOrientation 
    { 
    return YES; 
    } 

@end 

, 내가로 이동하기 전에 인터넷을 확인하기 위해 IBAction를 사용해야합니다 :

#import <UIKit/UIKit.h> 
#import "Reachability.h" 

@interface MainPageViewController : UIViewController 
{ 
    Reachability *internetReachable; 
    Reachability *hostReachable; 
} 

-(void) checkNetworkStatus: (NSNotification *)notice; 
@property BOOL internetActive; 
@property BOOL hostActive; 

@end 

가 여기 내하는 .m 파일 것 : 여기 내 .H 파일의 단편입니다 다음 페이지?

답변

0

어디서나 인터넷을 확인할 수 있습니다. viewDidLoad 또는 didFinishLaunching 메소드에서 확인하거나 인터넷 연결이 필요할 직전에 점검하십시오. "인터넷을 사용할 수 있습니까?" 버튼을 추가로 누르지 않아도됩니다.

다음은 연결 여부를 반환하는 함수입니다. 나는 대개 앱의 프로세스 동안 (또는 내가 편집증이라면 모든 요청 전에) 이것을 매번 실행한다.

- (BOOL) connectedToNetwork 
{ 
    Reachability *r = [Reachability reachabilityWithHostName:@"www.google.com"]; 
    NetworkStatus internetStatus = [r currentReachabilityStatus]; 
    BOOL internet; 
    if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) { 
     internet = NO; 
    } else { 
     internet = YES; 
    } 
    return internet; 
} 

그런 다음 (현재 객체에서 그것을 참조하는 가정)과 같이 사용할 수 있습니다 :

if(![self connectedToNetwork]) { 
    //Do whatever you need to if you're not connected 
} else { 
    // You're connected so let the games begin 
} 
+0

을 그래서의 두 번째 블록을 넣어 않는 경우의 viewDidLoad 방법에 문? – bumpfox

관련 문제