2016-11-30 113 views
1
-(BOOL)IsConnectionAvailable{ 
    Reachability *reachability = [Reachability reachabilityForInternetConnection]; 
    NetworkStatus networkStatus = [reachability currentReachabilityStatus]; 
    return !(networkStatus == NotReachable); 
} 

我正在使用上面的代碼,並且必須定期調用以檢查是否有網絡連接。 我正在聊天應用程序,我想立即發送消息一個設備連接到網絡。自動檢測我的手機是否連接到互聯網

感謝

+3

指這一點。 http://stackoverflow.com/questions/17804830/how-to-get-change-in-network-connection-notification-from-ios-reachability-class – Wolverine

回答

0

可以使用kReachabilityChangedNotification

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkForReachability:) name:kReachabilityChangedNotification object:nil]; 

} 

- (void) checkForReachability:(NSNotification *)notification 
{ 
    if([self IsConnectionAvailable])// use your method here 
    { 
     // net available 
    } 
    else 
    { 
     // no internet 
    } 

}