I want get a call when network is change of iPhone.
I Use Reachability.m of Apple guide like this:
xxx.m
struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;
Reachability = [self reachabilityWithAddress: (const struct sockaddr *) &zeroAddress]
Reachability.m
BOOL returnValue = NO;
SCNetworkReachabilityContext context = {0, (__bridge void *)(self), NULL, NULL, NULL};
if (SCNetworkReachabilitySetCallback(_reachabilityRef, ReachabilityCallback, &context))
{
if (SCNetworkReachabilityScheduleWithRunLoop(_reachabilityRef, CFRunLoopGetMain(), kCFRunLoopDefaultMode))
{
returnValue = YES;
}
}
return returnValue;
xxx.m
ReachabilityCallback {
//do something when network change
}
I test change mobile(4G) to wifi1
recives call like
Status4G
StatusNone
StatusWifi1
When change form wifi1 to wifi2
StatusWifi1
StatusNone
StatusWifi2
However, sometimes, when wifi changes very fast and did not change to StatusNone
, I did not get callback when i change wifi1 to wifi2 or the other way round.
What i want get is
StatusWifi1
StatusWifi2
-------------------------UPDATE----------------------------
Thanks @Hitesh Surani, now my question is sometimes on some devices, i did not received disconnect state
, I try to use
[self reachabilityWithAddress: @"www.google.com"]
Replace
[self reachabilityWithAddress: (const struct sockaddr *) &zeroAddress]
Now, I can received call back when wifi is change even it did not into disconnect state
, but i still didn't known why, here is the state change:
//wifi1:
1 kSCNetworkReachabilityFlagsReachable
//wifi1 -> wifi2
2 kSCNetworkReachabilityFlagsReachable | kSCNetworkReachabilityFlagsTransientConnection
3 kSCNetworkReachabilityFlagsReachable
when change wifi, there is tmp state kSCNetworkReachabilityFlagsReachable | kSCNetworkReachabilityFlagsTransientConnection
, that's why i can detect wifi changes, but what does kSCNetworkReachabilityFlagsTransientConnection
mean? i read the doc of Apple, still be confused.