在我的应用程序中,我正在使用网络服务。我已经使用 Reachability 类实现了网络可达性。但这仅在我启动我的应用程序时检查。
请注意,如果发生信号丢失或站点关闭,则在下载数据时,我想显示警报窗口。
这可以通过使用可达性类来完成吗?如果是,那么如何实施?如果没有,其他实施方式是什么?
提前致谢。
在我的应用程序中,我正在使用网络服务。我已经使用 Reachability 类实现了网络可达性。但这仅在我启动我的应用程序时检查。
请注意,如果发生信号丢失或站点关闭,则在下载数据时,我想显示警报窗口。
这可以通过使用可达性类来完成吗?如果是,那么如何实施?如果没有,其他实施方式是什么?
提前致谢。
是的,您可以在以下帮助下做到这一点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];
}
是的,它可以做到。一旦您注册以获取状态,任何更改都会触发回调。相应地处理它。