【发布时间】: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