2013-04-25 1 views
1

Im new in iphone 그리고 xcode에서 Reachability를 사용하는 방법을 알고 싶습니다. 나는 Reachability example로 가서 그것에 대해 읽었지만 그것에 대해 이해합니다. 하나의 응용 프로그램을 만들고 Reachability.m과 Reachability.h를 넣었지만 사용 방법을 모릅니다.평가를 위해 xcode에서 도달 가능성을 사용하는 방법

나를 안내 해주세요. 그 값을 검사함으로써 internetStatus VAR을 확인 지금

Reachability *reachability = [Reachability reachabilityForInternetConnection]; 
NetworkStatus internetStatus = [reachability currentReachabilityStatus]; 

그리고 :

if (isConnection) 
{ 
NSLog(@"Connection Success") 
} 
else 
NSLog(@"Connection has been lost") 

답변

2

당신은이 작업을 수행 할 수 있습니다 실행 응용 프로그램 내 네트워크 연결 언제든지 확인하고이 코드를 실행하면 내가 원하는. 귀하의 경우,

typedef enum 
{ 
    // Apple NetworkStatus Compatible Names. 
    NotReachable  = 0, 
    ReachableViaWiFi = 2, 
    ReachableViaWWAN = 1 
} NetworkStatus; 

: 그래서

if (internetStatus == NotReachable) 
{ 
    NSLog(@"Bazinga!"); 
} 
else 
{ 
    NSLog(@"Houston we have ignition"); 
} 
+0

Reachability.m 및 Reachability.h를 추가 할 때 내 친구는 많은 오류가 발생합니다 !!! 왜? – fred

+0

아마도'SystemConfiguration' 프레임 워크를 추가하는 것을 잊었을 것입니다. – Peres

+0

아니요 SystemConfiguration을 추가했지만 많은 오류가 발생했습니다 – fred

0

다운로드 도달 가능성 클래스와 우리가 만든 NetworkStatus 변수를 설정합니다

internetReach = [[Reachability reachabilityForInternetConnection] retain]; 
[internetReach startNotifier]; 

그런 다음이 코드를 따르 값으로 정의된다 도달 가능성.

NetworkStatus netStatus = [internetReach currentReachabilityStatus]; 

마지막으로 스위치 블록에서 netStatus를 사용합니다.

switch (netStatus) 
{    
    case ReachableViaWWAN: 
    { 
     break; 
    } 
    case ReachableViaWiFi: 
    { 
     break; 
    } 
    case NotReachable: 
    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"We are unable to make a internet connection at this time. Some functionality will be limited until a connection is made." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
     [alert show]; 
     [alert release]; 
     break; 
    } 

} 

- (void) reachabilityChanged: (NSNotification*)note 
{ 
    Reachability* curReach = [note object]; 
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]); 

    NetworkStatus netStatus = [curReach currentReachabilityStatus]; 
    switch (netStatus) 
    { 
     case ReachableViaWWAN: 
     { 
      break; 
     } 
     case ReachableViaWiFi: 
     { 
      break; 
     } 
     case NotReachable: 
     { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"We are unable to make a internet connection at this time. Some functionality will be limited until a connection is made." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
      [alert show]; 
      [alert release]; 
      break; 
     } 
    } 
} 
+0

내 친구 internetReach 무엇입니까 ??? 포인터? 클래스에서 ?? – fred

+0

reachabilityChanged 메서드를 쓸 때 내 친구가 오류가 발생합니다 !!! – fred

관련 문제