2016-11-30 110 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 
    } 

}