OGeek|极客世界-中国程序员成长平台

标题: ios - 使用 Reachability 类在 ios 中检查整个应用程序中的事件互联网连接 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 09:33
标题: ios - 使用 Reachability 类在 ios 中检查整个应用程序中的事件互联网连接

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

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

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

}

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


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

这适用于特定的 View Controller 。新的 Reachability 类中是否还有其他方法可以检查整个应用程序的运行情况?还是我必须在每个 View Controller 中执行此检查以检查事件的 Internet 连接?



Best Answer-推荐答案


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

@property (strong,nonatomic)Reachability *reach;
@property(nonatomic)NetworkStatus netStatus;

- (BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)launchOptions
{
[[NSNotificationCenter defaultCenter] addObserver:self
                                             selectorselector(checkNetworkStatus
                                                 name:kReachabilityChangedNotification object:nil];
    reach = [Reachability reachabilityForInternetConnection];
    [reach startNotifier];

    [self checkNetworkStatus:nil];
}

- (void)checkNetworkStatusNSNotification *)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];
    }
}

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

======== 编辑

在应用委托(delegate).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
}

关于ios - 使用 Reachability 类在 ios 中检查整个应用程序中的事件互联网连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26250944/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4