在离线模式下,我在数据库中有一些数据。我想在设备上线后与服务器同步。我正在尝试 Reachability
。但是我没有从 Reachability
得到任何响应。我正在尝试下面的代码,但它不起作用。
我在 ViewDidiLoad
中调用这个方法:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
然后调用这个方法:
-(BOOL)reachabilityChanged:(NSNotification*)note
{
BOOL status =YES;
Reachability * reach = [note object];
if ([reach currentReachabilityStatus] == NotReachable )
{
status = NO;
NSLog(@"NetWork is not Available");
}
else
{
status = YES;
NSLog(@"NetWork is Available");
}
return status;
但是什么也没有发生。请任何人建议我。我的要求是当网络到来时,数据库数据将自动与服务器同步。无需触摸设备。
在此先感谢...
最佳答案
我已经使用可达性类进行网络检查,例如 在 ViewDidload 方法中添加以下行:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(checkNetworkStatus:)
name:kReachabilityChangedNotification
object:nil];
kReachabilityChangedNotification 在 Reachability 类中设置。
同时添加以下方法:
-(void) checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
NSLog(@"The internet is down.");
break;
}
case ReachableViaWiFi:
{
NSLog(@"The internet is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN.");
break;
}
}
}
我已经使用上面的代码进行网络检查。
希望对您有所帮助。
关于ios - 如何在 iOS 网络接入时自动与服务器同步?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31980190/