Developer Area

Show
Ignore:
Timestamp:
09/06/11 10:22:08 (3 years ago)
Author:
henri
Message:

Fixed issues related to adding ShareKit?

Location:
apps/iphone/superbook/trunk/ShareKit
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • apps/iphone/superbook/trunk/ShareKit

    • Property svn:ignore set to
      SHKConfig.h
  • apps/iphone/superbook/trunk/ShareKit/Reachability/Reachability.m

    r892 r893  
    44 Abstract: Basic demonstration of how to use the SystemConfiguration Reachablity APIs. 
    55  
    6  Version: 2.1 
     6 Version: 2.2 
    77  
    88 Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple Inc. 
     
    4444 Copyright (C) 2010 Apple Inc. All Rights Reserved. 
    4545  
    46 */ 
     46 */ 
    4747 
    4848#import <sys/socket.h> 
     
    5757#import "Reachability.h" 
    5858 
    59 #define kShouldPrintReachabilityFlags 0 
     59#define kShouldPrintReachabilityFlags 1 
    6060 
    6161static void PrintReachabilityFlags(SCNetworkReachabilityFlags    flags, const char* comment) 
    6262{ 
    6363#if kShouldPrintReachabilityFlags 
    64          
     64     
    6565    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                         comment 
    77                         ); 
     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          ); 
    7878#endif 
    7979} 
     
    8383static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info) 
    8484{ 
    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]; 
     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]; 
    9898} 
    9999 
    100100- (BOOL) startNotifier 
    101101{ 
    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; 
     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; 
    112112} 
    113113 
    114114- (void) stopNotifier 
    115115{ 
    116         if(reachabilityRef!= NULL) 
    117         { 
    118                 SCNetworkReachabilityUnscheduleFromRunLoop(reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); 
    119         } 
     116    if(reachabilityRef!= NULL) 
     117    { 
     118        SCNetworkReachabilityUnscheduleFromRunLoop(reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); 
     119    } 
    120120} 
    121121 
    122122- (void) dealloc 
    123123{ 
    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]; 
    130130} 
    131131 
    132132+ (Reachability*) reachabilityWithHostName: (NSString*) hostName; 
    133133{ 
    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; 
    146146} 
    147147 
    148148+ (Reachability*) reachabilityWithAddress: (const struct sockaddr_in*) hostAddress; 
    149149{ 
    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; 
    162162} 
    163163 
    164164+ (Reachability*) reachabilityForInternetConnection; 
    165165{ 
    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]; 
    171171} 
    172172 
    173173+ (Reachability*) reachabilityForLocalWiFi; 
    174174{ 
    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; 
     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; 
    187187} 
    188188 
     
    191191- (NetworkStatus) localWiFiStatusForFlags: (SCNetworkReachabilityFlags) flags 
    192192{ 
    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; 
    201201} 
    202202 
    203203- (NetworkStatus) networkStatusForFlags: (SCNetworkReachabilityFlags) flags 
    204204{ 
    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; 
     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; 
    242242} 
    243243 
    244244- (BOOL) connectionRequired; 
    245245{ 
    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; 
    253253} 
    254254 
    255255- (NetworkStatus) currentReachabilityStatus 
    256256{ 
    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; 
     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; 
    272272} 
    273273@end 
Telnic
Search This Site
Partners
Neustar
ICANN
Main site | WHOIS | Sell .tel | FAQ | Archived Site | About Telnic | Contact Us