ios - 如何在 iOS 网络接入时自动与服务器同步?

标签 ios

在离线模式下,我在数据库中有一些数据。我想在设备上线后与服务器同步。我正在尝试 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/

相关文章:

ios - 检测 UITextView 上的点击仅检测取消

ios - 如何在单个选择器中管理国家/州/城市?

ios - tableView 重新加载时崩溃

ios - 上推和下拉 UIView

ios - ITMS-90809 : Deprecated API Usage — Apple will stop accepting submissions of apps that use UIWebView APIs When upload myApp

iOS Facebook sdk邀请好友请求已发送但未收到好友通知

ios - 以编程方式快速制作二维数组

ios - Objective-C 中是否可以在一组其他方法执行完成后调用一个方法

ios - 为什么 Storyboard应用程序中的 self.navigationController 为 nil?

iphone - 如何从嵌套的 UIViewController 中获取正确的 UIInterfaceOrientation