2012-09-27 3 views
2

Wi-Fi 연결이 설정되거나 연결이 끊어지면 iOS에 내 앱을 알리려고합니다. iOS에서 Wi-Fi가 연결 또는 연결 해제되면 알림을 받으려면 어떻게해야하나요?

나는 아이폰 OS 4.3.5 및 5.1로 아이폰과 아이 패드에서 다음 코드를 시도 :

- (void)startWifiNotifier { 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(statusUpdated:) 
               name:kReachabilityChangedNotification 
               object:nil]; 

    Reachability *wifi = [Reachability reachabilityForLocalWiFi]; 
    [wifi startNotifier]; 
    NSLog(@"Wifi status: %i", [wifi currentReachabilityStatus]); 
    NSLog(@"Notifier started"); 
} 

- (void)statusUpdated:(NSNotification *)notice { 
    NetworkStatus s = [[notice object] currentReachabilityStatus]; 
    if(s == NotReachable) { 
     NSLog(@"Wifi disconnected"); 
    } else { 
     NSLog(@"Wifi connection established"); 
    } 
} 

문제는 내 콜백 "statusUpdated는"결코 호출되지 가져옵니다이다. 나는 이것이 반드시 작동해야 함을 알고 있지만 wifi 범위를 벗어나서 (따라서 연결되어 있고 연결이 끊어짐) wifi 라우터를 켜고 끄고 연결 해제를 시도하고 콜백이 호출되지 않습니다.

@private 
    Reachability *wifi; 
... 

- (void) startTimerLog { 
    wifi = [Reachability reachabilityForLocalWiFi]; 
    [self timerLog]; 
} 

- (void) timerLog { 
    NSLog(@"Reachability: %i", [wifi currentReachabilityStatus]); 
    [self performSelector:@selector(timerLog) withObject:nil afterDelay:0.5];  
} 

과 같은 시나리오에서 그것을 테스트하고 그것을 잘 작동합니다 :

나는 또한이 시도. 그러나 그것은 내가 필요한 접근 방식이 아니며 오히려 콜백을 호출하기위한 알림이 필요합니다.

알리미를 작동시키는 데 필요한 다른 것이 있습니까?

+0

Reachability 프레임 워크에 대한 Apple의 샘플 코드를 살펴보십시오. 나는 필요한 알림을 모두 받았지만 위의 코드보다 앱에 더 많은 코드가 있습니다. 이것은 복잡한 작업이며 단계를 건너 뛰면 문제가 발생할 수 있습니다. –

답변

1

이 라이브러리를 사용하면 http://huytd.github.io/datatify/ 매우 쉽게 사용하고 사용자 지정할 수 있습니다. setCallback 메서드는 연결이 변경된시기를 감지하는 데 도움이됩니다.

관련 문제