• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 使用 Reachability 类在 ios 中检查整个应用程序中的事件互联网连接

[复制链接]
菜鸟教程小白 发表于 2022-12-13 09:33:28 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在构建一个应用程序,只要互联网连接处于事件状态,它就需要将离线数据与服务器同步。因此,目前如果在将数据推送到服务器时互联网连接丢失,它将保存在数据库中,并且只要连接处于事件状态,它就会将数据推送到服务器。我正在使用来自苹果的新可达性类版本: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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap