【问题标题】:Check for active internet connection throughout the app in ios using Reachability class使用 Reachability 类在 ios 中检查整个应用程序中的活动互联网连接
【发布时间】:2014-12-02 18:17:02
【问题描述】:

我正在构建一个应用程序,只要互联网连接处于活动状态,它就需要将离线数据与服务器同步。因此,目前如果在将数据推送到服务器时互联网连接丢失,它将保存在数据库中,并且只要连接处于活动状态,它就会将数据推送到服务器。我正在使用来自苹果的新可达性类版本:3.5。根据他们针对特定视图控制器的示例,我可以这样做

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

    self.internetReachability = [Reachability reachabilityForInternetConnection];
    [self.internetReachability startNotifier];
    [self updateInterfaceWithReachability:self.internetReachability];

}

/*!
 * Called by Reachability whenever status changes.
 */
- (void) reachabilityChanged:(NSNotification *)note
{
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
    [self updateInterfaceWithReachability:curReach];
}


- (void)updateInterfaceWithReachability:(Reachability *)reachability
{
   if (reachability == self.internetReachability)
    {
     //Internet is active again- call api to push data to server
     }
}

这适用于特定的视图控制器。新的 Reachability 类中是否还有其他方法可以检查整个应用程序的运行情况?还是我必须在每个视图控制器中执行此检查以检查活动的互联网连接?

【问题讨论】:

    标签: ios ipad ios7 reachability


    【解决方案1】:

    将数据推送到服务器是在应用程序委托中完成的,而不是在特定的视图控制器中。这样,您的可访问代码将在 Appdelegate 中,您可以简单地从视图控制器调用 Appdelegate 的方法来保存数据或推送到服务器。

    如果您不想使用 Appdelegate,请为此目的使用单例类

    【讨论】:

      【解决方案2】:

      我与其他人合作的项目使用了一个使用单例模式的 ReachabilityManager,如 Reachability 所述

      您可以创建一个 UIViewController 的子类,在 viewdidload 中添加对通知的观察,并且所有视图控制器都是此视图控制器的子类。

      【讨论】:

        【解决方案3】:

        一种方法是创建一个 parent UIViewController 类来处理可达性的变化,父类将被其余的 viewController 类继承。

        另一种方法是创建一个处理可达性的类,在应用启动时实例化并通过通知通知或委托或阻止对可达性感兴趣的视图控制器、通信和数据库处理程序。

        也可以通过直接在 UIApplication 的 keyWindow 中添加 subview 来通知用户离线。

        【讨论】:

          【解决方案4】:

          你可以通过appdelegate查看。我以前也这样做过。

          @property (strong,nonatomic)Reachability *reach;
          @property(nonatomic)NetworkStatus netStatus;
          
          - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
          {
          [[NSNotificationCenter defaultCenter] addObserver:self
                                                       selector:@selector(checkNetworkStatus:)
                                                           name:kReachabilityChangedNotification object:nil];
              reach = [Reachability reachabilityForInternetConnection];
              [reach startNotifier];
          
              [self checkNetworkStatus:nil];
          }
          
          - (void)checkNetworkStatus:(NSNotification *)notice
          {
              netStatus = [reach currentReachabilityStatus];
              if (netStatus == NotReachable)
              {
                    NSLog(@"The internet is down.");
                   // do stuff when network gone. 
              }
              else
              {
                    NSLog(@"The internet is working!");
                    // do stuff when internet comes active 
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"INTERNET_AVAILABLE" object:nil];
              }
          }
          

          现在,当互联网出现时,它会通知。在您需要检查互联网连接的所有视图中添加通知观察者。它正在检查整个应用程序的互联网。并且属性被合成。

          ======== 编辑

          在应用委托.h

          + (BOOL)isActiveInternet;
          

          在应用中的delegate.m

          + (BOOL)isActiveInternet
              {
                  netStatus = [reach currentReachabilityStatus];
                  if (netStatus == NotReachable)
                  {
                        NSLog(@"The internet is down.");
                       // do stuff when network gone. 
                        return FALSE;
                  }
                  else
                  {
                        NSLog(@"The internet is working!");
                        // do stuff when internet comes active 
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"INTERNET_AVAILABLE" object:nil];
                        return TRUE;
                  }
              }
          

          这样您就可以在项目中的任何位置直接调用此方法,例如

          if([appdelegate isActiveInternet]) {
                //yes net available do your stuff
          }
          

          【讨论】:

          • Max 如何检查每个视图控制器。
          • @UmaMadhavi 您可以将其保存到您的任何对象,或者您可以在应用程序委托方法中通过 true 或 false 返回当前的互联网状态,然后调用该方法进行检查。您可以在 checkNetworkStatus 中返回 true 或 false,并在需要检查时调用它。
          • 我没有得到你。在应用程序委托的情况下效果很好。但是如果我想使用 ViewController1 那么我该如何实现
          • @UmaMadhavi.. 让我为您编辑答案并更好地理解这一点。
          • [[NSNotificationCenter defaultCenter] postNotificationName:@"INTERNET_AVAILABLE" object:nil];在这个互联网上可用意味着我应该定义什么
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-08-30
          • 2014-05-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-01-02
          • 1970-01-01
          相关资源
          最近更新 更多