我将可达性代码合并到我的应用程序中,它已经成功运行了几个月,但到目前为止我只在 iOS 5 设备上使用过它。但是它不适用于 iOS 4 设备。
通知注册如下:
- (id) init
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChange:)
name:kReachabilityChangedNotification
object:nil];
...
- (void) appBecameActive
{
...
self.reachability = [Reachability reachabilityWithHostName:[url host]];
...
[self.reachability startNotifier];
stopNotifier 只在 dealloc 中被调用
问题是当可达性发生变化时,ReachabilityCallback 没有被回调,但我看不出它在 iOS5 上应该没问题但在 iOS4 上没问题的任何理由。
这部分代码与 Apple 示例源代码相同:
static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void *info) {
#pragma unused (target, flags)
NSCAssert(info != NULL, @"info was NULL in ReachabilityCallback");
NSCAssert([(__bridge id)info isKindOfClass:[Reachability class]], @"info was wrong class in ReachabilityCallback");
Reachability *noteObject = (__bridge Reachability *)info;
// Post a notification to notify the client that the network reachability changed.
[[NSNotificationCenter defaultCenter] postNotificationName:kReachabilityChangedNotification object:noteObject];
}
如前所述,在 iOS5 设备上运行时一切都运行良好,过去有没有其他人遇到过类似的问题,将可达性代码与 iOS4 结合起来?