如何针对信号丢失或站点关闭实施代码?

发布于 2024-11-26 17:25:15 字数 193 浏览 6 评论 0原文

在我的应用程序中,我使用网络服务。我已经使用 Reachability 类实现了网络可达性。但只有当我开始我的应用程序时才会检查。

请注意,在下载数据时,如果发生信号丢失或站点关闭,我想显示警报窗口。

这可以通过使用 Reachability 类来完成吗?如果是,那么如何实施?如果不是,那么其他实施方式是什么?

提前致谢。

In my app I am using webservices. I have implemented the Network Reachability using Reachability class. But that checks only when i start my application.

Note that, while downloading the data if loss of signal or site down occurs, I want to show the alert window.

Can this be done with the use of Reachability class? If yes than how to implement this? If no than what is the other way to implement ?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拔了角的鹿 2024-12-03 17:25:15

是的,您可以在 NSNotificationCenter 的帮助下完成此操作

- (void) addReachability
{
    //Use the Reachability class to determine if the internet can be reached.
        [[Reachability sharedReachability] setHostName:kHostName];

    //Set Reachability class to notifiy app when the network status changes.
    [[Reachability sharedReachability] setNetworkStatusNotificationsEnabled:YES];

    //Set a method to be called when a notification is sent.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) 
                                                 name:@"kNetworkReachabilityChangedNotification" object:nil];

}

- (void)reachabilityChanged:(NSNotification *)note 
{
    [AppDelegate updateStatus];
}

Yes you can do this with help of NSNotificationCenter

- (void) addReachability
{
    //Use the Reachability class to determine if the internet can be reached.
        [[Reachability sharedReachability] setHostName:kHostName];

    //Set Reachability class to notifiy app when the network status changes.
    [[Reachability sharedReachability] setNetworkStatusNotificationsEnabled:YES];

    //Set a method to be called when a notification is sent.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) 
                                                 name:@"kNetworkReachabilityChangedNotification" object:nil];

}

- (void)reachabilityChanged:(NSNotification *)note 
{
    [AppDelegate updateStatus];
}
执妄 2024-12-03 17:25:15

是的,这是可以做到的。一旦您注册以获取状态,任何更改都会触发回调。相应地处理它。

Yes it can be done. Once you register to get the status any change will hit the callback. Handle it accordingly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文