- Timestamp:
- 09/06/11 10:22:08 (22 months ago)
- Location:
- apps/iphone/superbook/trunk/ShareKit
- Files:
-
- 2 modified
-
. (modified) (1 prop)
-
Reachability/Reachability.m (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
apps/iphone/superbook/trunk/ShareKit
-
Property
svn:ignore set
to
SHKConfig.h
-
Property
svn:ignore set
to
-
apps/iphone/superbook/trunk/ShareKit/Reachability/Reachability.m
r892 r893 4 4 Abstract: Basic demonstration of how to use the SystemConfiguration Reachablity APIs. 5 5 6 Version: 2. 16 Version: 2.2 7 7 8 8 Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc. … … 44 44 Copyright (C) 2010 Apple Inc. All Rights Reserved. 45 45 46 */46 */ 47 47 48 48 #import <sys/socket.h> … … 57 57 #import "Reachability.h" 58 58 59 #define kShouldPrintReachabilityFlags 059 #define kShouldPrintReachabilityFlags 1 60 60 61 61 static void PrintReachabilityFlags(SCNetworkReachabilityFlags flags, const char* comment) 62 62 { 63 63 #if kShouldPrintReachabilityFlags 64 64 65 65 NSLog(@"Reachability Flag Status: %c%c %c%c%c%c%c%c%c %s\n", 66 (flags & kSCNetworkReachabilityFlagsIsWWAN)? 'W' : '-',67 (flags & kSCNetworkReachabilityFlagsReachable) ? 'R' : '-',68 69 (flags & kSCNetworkReachabilityFlagsTransientConnection) ? 't' : '-',70 (flags & kSCNetworkReachabilityFlagsConnectionRequired) ? 'c' : '-',71 (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) ? 'C' : '-',72 (flags & kSCNetworkReachabilityFlagsInterventionRequired) ? 'i' : '-',73 (flags & kSCNetworkReachabilityFlagsConnectionOnDemand) ? 'D' : '-',74 (flags & kSCNetworkReachabilityFlagsIsLocalAddress) ? 'l' : '-',75 (flags & kSCNetworkReachabilityFlagsIsDirect) ? 'd' : '-',76 comment77 );66 (flags & kSCNetworkReachabilityFlagsIsWWAN) ? 'W' : '-', 67 (flags & kSCNetworkReachabilityFlagsReachable) ? 'R' : '-', 68 69 (flags & kSCNetworkReachabilityFlagsTransientConnection) ? 't' : '-', 70 (flags & kSCNetworkReachabilityFlagsConnectionRequired) ? 'c' : '-', 71 (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) ? 'C' : '-', 72 (flags & kSCNetworkReachabilityFlagsInterventionRequired) ? 'i' : '-', 73 (flags & kSCNetworkReachabilityFlagsConnectionOnDemand) ? 'D' : '-', 74 (flags & kSCNetworkReachabilityFlagsIsLocalAddress) ? 'l' : '-', 75 (flags & kSCNetworkReachabilityFlagsIsDirect) ? 'd' : '-', 76 comment 77 ); 78 78 #endif 79 79 } … … 83 83 static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info) 84 84 { 85 #pragma unused (target, flags)86 NSCAssert(info != NULL, @"info was NULL in ReachabilityCallback");87 NSCAssert([(NSObject*) info isKindOfClass: [Reachability class]], @"info was wrong class in ReachabilityCallback");88 89 //We're on the main RunLoop, so an NSAutoreleasePool is not necessary, but is added defensively90 // in case someon uses the Reachablity object in a different thread.91 NSAutoreleasePool* myPool = [[NSAutoreleasePool alloc] init];92 93 Reachability* noteObject = (Reachability*) info;94 // Post a notification to notify the client that the network reachability changed.95 [[NSNotificationCenter defaultCenter] postNotificationName: kReachabilityChangedNotification object: noteObject];96 97 [myPool release];85 #pragma unused (target, flags) 86 NSCAssert(info != NULL, @"info was NULL in ReachabilityCallback"); 87 NSCAssert([(NSObject*) info isKindOfClass: [Reachability class]], @"info was wrong class in ReachabilityCallback"); 88 89 //We're on the main RunLoop, so an NSAutoreleasePool is not necessary, but is added defensively 90 // in case someon uses the Reachablity object in a different thread. 91 NSAutoreleasePool* myPool = [[NSAutoreleasePool alloc] init]; 92 93 Reachability* noteObject = (Reachability*) info; 94 // Post a notification to notify the client that the network reachability changed. 95 [[NSNotificationCenter defaultCenter] postNotificationName: kReachabilityChangedNotification object: noteObject]; 96 97 [myPool release]; 98 98 } 99 99 100 100 - (BOOL) startNotifier 101 101 { 102 BOOL retVal = NO;103 SCNetworkReachabilityContextcontext = {0, self, NULL, NULL, NULL};104 if(SCNetworkReachabilitySetCallback(reachabilityRef, ReachabilityCallback, &context))105 {106 if(SCNetworkReachabilityScheduleWithRunLoop(reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode))107 {108 retVal = YES;109 }110 }111 return retVal;102 BOOL retVal = NO; 103 SCNetworkReachabilityContext context = {0, self, NULL, NULL, NULL}; 104 if(SCNetworkReachabilitySetCallback(reachabilityRef, ReachabilityCallback, &context)) 105 { 106 if(SCNetworkReachabilityScheduleWithRunLoop(reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode)) 107 { 108 retVal = YES; 109 } 110 } 111 return retVal; 112 112 } 113 113 114 114 - (void) stopNotifier 115 115 { 116 if(reachabilityRef!= NULL)117 {118 SCNetworkReachabilityUnscheduleFromRunLoop(reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);119 }116 if(reachabilityRef!= NULL) 117 { 118 SCNetworkReachabilityUnscheduleFromRunLoop(reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); 119 } 120 120 } 121 121 122 122 - (void) dealloc 123 123 { 124 [self stopNotifier];125 if(reachabilityRef!= NULL)126 {127 CFRelease(reachabilityRef);128 }129 [super dealloc];124 [self stopNotifier]; 125 if(reachabilityRef!= NULL) 126 { 127 CFRelease(reachabilityRef); 128 } 129 [super dealloc]; 130 130 } 131 131 132 132 + (Reachability*) reachabilityWithHostName: (NSString*) hostName; 133 133 { 134 Reachability* retVal = NULL;135 SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, [hostName UTF8String]);136 if(reachability!= NULL)137 {138 retVal= [[[self alloc] init] autorelease];139 if(retVal!= NULL)140 {141 retVal->reachabilityRef = reachability;142 retVal->localWiFiRef = NO;143 }144 }145 return retVal;134 Reachability* retVal = NULL; 135 SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, [hostName UTF8String]); 136 if(reachability!= NULL) 137 { 138 retVal= [[[self alloc] init] autorelease]; 139 if(retVal!= NULL) 140 { 141 retVal->reachabilityRef = reachability; 142 retVal->localWiFiRef = NO; 143 } 144 } 145 return retVal; 146 146 } 147 147 148 148 + (Reachability*) reachabilityWithAddress: (const struct sockaddr_in*) hostAddress; 149 149 { 150 SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr*)hostAddress);151 Reachability* retVal = NULL;152 if(reachability!= NULL)153 {154 retVal= [[[self alloc] init] autorelease];155 if(retVal!= NULL)156 {157 retVal->reachabilityRef = reachability;158 retVal->localWiFiRef = NO;159 }160 }161 return retVal;150 SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr*)hostAddress); 151 Reachability* retVal = NULL; 152 if(reachability!= NULL) 153 { 154 retVal= [[[self alloc] init] autorelease]; 155 if(retVal!= NULL) 156 { 157 retVal->reachabilityRef = reachability; 158 retVal->localWiFiRef = NO; 159 } 160 } 161 return retVal; 162 162 } 163 163 164 164 + (Reachability*) reachabilityForInternetConnection; 165 165 { 166 struct sockaddr_in zeroAddress;167 bzero(&zeroAddress, sizeof(zeroAddress));168 zeroAddress.sin_len = sizeof(zeroAddress);169 zeroAddress.sin_family = AF_INET;170 return [self reachabilityWithAddress: &zeroAddress];166 struct sockaddr_in zeroAddress; 167 bzero(&zeroAddress, sizeof(zeroAddress)); 168 zeroAddress.sin_len = sizeof(zeroAddress); 169 zeroAddress.sin_family = AF_INET; 170 return [self reachabilityWithAddress: &zeroAddress]; 171 171 } 172 172 173 173 + (Reachability*) reachabilityForLocalWiFi; 174 174 { 175 struct sockaddr_in localWifiAddress;176 bzero(&localWifiAddress, sizeof(localWifiAddress));177 localWifiAddress.sin_len = sizeof(localWifiAddress);178 localWifiAddress.sin_family = AF_INET;179 // IN_LINKLOCALNETNUM is defined in <netinet/in.h> as 169.254.0.0180 localWifiAddress.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM);181 Reachability* retVal = [self reachabilityWithAddress: &localWifiAddress];182 if(retVal!= NULL)183 {184 retVal->localWiFiRef = YES;185 }186 return retVal;175 struct sockaddr_in localWifiAddress; 176 bzero(&localWifiAddress, sizeof(localWifiAddress)); 177 localWifiAddress.sin_len = sizeof(localWifiAddress); 178 localWifiAddress.sin_family = AF_INET; 179 // IN_LINKLOCALNETNUM is defined in <netinet/in.h> as 169.254.0.0 180 localWifiAddress.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM); 181 Reachability* retVal = [self reachabilityWithAddress: &localWifiAddress]; 182 if(retVal!= NULL) 183 { 184 retVal->localWiFiRef = YES; 185 } 186 return retVal; 187 187 } 188 188 … … 191 191 - (NetworkStatus) localWiFiStatusForFlags: (SCNetworkReachabilityFlags) flags 192 192 { 193 PrintReachabilityFlags(flags, "localWiFiStatusForFlags");194 195 BOOL retVal = NotReachable;196 if((flags & kSCNetworkReachabilityFlagsReachable) && (flags & kSCNetworkReachabilityFlagsIsDirect))197 {198 retVal = ReachableViaWiFi; 199 }200 return retVal;193 PrintReachabilityFlags(flags, "localWiFiStatusForFlags"); 194 195 BOOL retVal = NotReachable; 196 if((flags & kSCNetworkReachabilityFlagsReachable) && (flags & kSCNetworkReachabilityFlagsIsDirect)) 197 { 198 retVal = ReachableViaWiFi; 199 } 200 return retVal; 201 201 } 202 202 203 203 - (NetworkStatus) networkStatusForFlags: (SCNetworkReachabilityFlags) flags 204 204 { 205 PrintReachabilityFlags(flags, "networkStatusForFlags");206 if ((flags & kSCNetworkReachabilityFlagsReachable) == 0)207 {208 // if target host is not reachable209 return NotReachable;210 }211 212 BOOL retVal = NotReachable;213 214 if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0)215 {216 // if target host is reachable and no connection is required217 // then we'll assume (for now) that your on Wi-Fi218 retVal = ReachableViaWiFi;219 }220 221 222 if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) ||223 (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0))224 {225 // ... and the connection is on-demand (or on-traffic) if the226 // calling application is using the CFSocketStream or higher APIs227 228 if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0)229 {230 // ... and no [user] intervention is needed231 retVal = ReachableViaWiFi;232 }233 }234 235 if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)236 {237 // ... but WWAN connections are OK if the calling application238 // is using the CFNetwork (CFSocketStream?) APIs.239 retVal = ReachableViaWWAN;240 }241 return retVal;205 PrintReachabilityFlags(flags, "networkStatusForFlags"); 206 if ((flags & kSCNetworkReachabilityFlagsReachable) == 0) 207 { 208 // if target host is not reachable 209 return NotReachable; 210 } 211 212 BOOL retVal = NotReachable; 213 214 if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0) 215 { 216 // if target host is reachable and no connection is required 217 // then we'll assume (for now) that your on Wi-Fi 218 retVal = ReachableViaWiFi; 219 } 220 221 222 if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) || 223 (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0)) 224 { 225 // ... and the connection is on-demand (or on-traffic) if the 226 // calling application is using the CFSocketStream or higher APIs 227 228 if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0) 229 { 230 // ... and no [user] intervention is needed 231 retVal = ReachableViaWiFi; 232 } 233 } 234 235 if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN) 236 { 237 // ... but WWAN connections are OK if the calling application 238 // is using the CFNetwork (CFSocketStream?) APIs. 239 retVal = ReachableViaWWAN; 240 } 241 return retVal; 242 242 } 243 243 244 244 - (BOOL) connectionRequired; 245 245 { 246 NSAssert(reachabilityRef != NULL, @"connectionRequired called with NULL reachabilityRef");247 SCNetworkReachabilityFlags flags;248 if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags))249 {250 return (flags & kSCNetworkReachabilityFlagsConnectionRequired);251 }252 return NO;246 NSAssert(reachabilityRef != NULL, @"connectionRequired called with NULL reachabilityRef"); 247 SCNetworkReachabilityFlags flags; 248 if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) 249 { 250 return (flags & kSCNetworkReachabilityFlagsConnectionRequired); 251 } 252 return NO; 253 253 } 254 254 255 255 - (NetworkStatus) currentReachabilityStatus 256 256 { 257 NSAssert(reachabilityRef != NULL, @"currentNetworkStatus called with NULL reachabilityRef");258 NetworkStatus retVal = NotReachable;259 SCNetworkReachabilityFlags flags;260 if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags))261 {262 if(localWiFiRef)263 {264 retVal = [self localWiFiStatusForFlags: flags];265 }266 else267 {268 retVal = [self networkStatusForFlags: flags];269 }270 }271 return retVal;257 NSAssert(reachabilityRef != NULL, @"currentNetworkStatus called with NULL reachabilityRef"); 258 NetworkStatus retVal = NotReachable; 259 SCNetworkReachabilityFlags flags; 260 if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) 261 { 262 if(localWiFiRef) 263 { 264 retVal = [self localWiFiStatusForFlags: flags]; 265 } 266 else 267 { 268 retVal = [self networkStatusForFlags: flags]; 269 } 270 } 271 return retVal; 272 272 } 273 273 @end








