Reachability
Reachability copied to clipboard
使用blocks 方法监听,当网络状态变化时,blocks会调用两次
我是使用cocoapod导入的 :pod "Reachability",'~>3.2' 我在didFinishLaunchingWithOptions方法中使用这个 Reachability* reach = [Reachability reachabilityWithHostname:@"www.baidu.com"];
// Set the blocks
reach.reachableBlock = ^(Reachability*reach)
{
// keep in mind this is called on a background thread
// and if you are updating the UI it needs to happen
// on the main thread, like this:
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"网络可用了");
});
};
reach.unreachableBlock = ^(Reachability*reach)
{
NSLog(@"网络连接失败了!");
};
// Start the notifier, which will cause the reachability object to retain itself!
[reach startNotifier];
当网络状态变化时,会调用两次blocks
reach 初始化了两遍, 所以调用了两次 ~
我也遇到了这个问题,但是应该没有初始化两次,很奇怪