Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'master' of git://github.com/shazron/phonegap

  • Loading branch information...
commit 23e3fed563301cbaf6cf4f21dc1eb5c7e7e12a10 2 parents c917751 + 167bbe8
Brock Whitten sintaxi authored
5 Makefile
@@ -43,7 +43,7 @@ iphone: lib/iphone/phonegap-min.js
43 43 lib/iphone/phonegap-min.js: lib/iphone/phonegap.js
44 44 $(JAVA) -jar util/yuicompressor-2.4.2.jar --charset UTF-8 -o $@ lib/iphone/phonegap.js
45 45
46   -lib/iphone/phonegap.js: javascripts/phonegap.js.base javascripts/acceleration.js javascripts/accelerometer.js javascripts/camera.js javascripts/contact.js javascripts/debugconsole.js javascripts/device.js javascripts/file.js javascripts/geolocation.js javascripts/compass.js javascripts/map.js javascripts/media.js javascripts/notification.js javascripts/orientation.js javascripts/position.js javascripts/sms.js javascripts/telephony.js javascripts/uicontrols.js javascripts/iphone/accelerometer.js javascripts/iphone/bonjour.js javascripts/iphone/contact.js javascripts/iphone/camera.js javascripts/iphone/debugconsole.js javascripts/iphone/device.js javascripts/iphone/geolocation.js javascripts/iphone/compass.js javascripts/iphone/media.js javascripts/iphone/notification.js javascripts/iphone/phonegap.js javascripts/iphone/uicontrols.js
  46 +lib/iphone/phonegap.js: javascripts/phonegap.js.base javascripts/acceleration.js javascripts/accelerometer.js javascripts/camera.js javascripts/contact.js javascripts/debugconsole.js javascripts/device.js javascripts/file.js javascripts/geolocation.js javascripts/compass.js javascripts/map.js javascripts/media.js javascripts/notification.js javascripts/orientation.js javascripts/position.js javascripts/sms.js javascripts/telephony.js javascripts/uicontrols.js javascripts/network.js javascripts/iphone/accelerometer.js javascripts/iphone/bonjour.js javascripts/iphone/contact.js javascripts/iphone/camera.js javascripts/iphone/debugconsole.js javascripts/iphone/device.js javascripts/iphone/geolocation.js javascripts/iphone/compass.js javascripts/iphone/media.js javascripts/iphone/notification.js javascripts/iphone/phonegap.js javascripts/iphone/uicontrols.js javascripts/iphone/network.js
47 47 $(RM_RF) lib/iphone
48 48 $(MKPATH) lib/iphone
49 49 $(RM_F) $@
@@ -65,6 +65,7 @@ lib/iphone/phonegap.js: javascripts/phonegap.js.base javascripts/acceleration.js
65 65 $(CAT) javascripts/sms.js >> $@
66 66 $(CAT) javascripts/telephony.js >> $@
67 67 $(CAT) javascripts/uicontrols.js >> $@
  68 + $(CAT) javascripts/network.js >> $@
68 69 $(CAT) javascripts/iphone/accelerometer.js >> $@
69 70 $(CAT) javascripts/iphone/bonjour.js >> $@
70 71 $(CAT) javascripts/iphone/contact.js >> $@
@@ -77,6 +78,8 @@ lib/iphone/phonegap.js: javascripts/phonegap.js.base javascripts/acceleration.js
77 78 $(CAT) javascripts/iphone/notification.js >> $@
78 79 $(CAT) javascripts/iphone/phonegap.js >> $@
79 80 $(CAT) javascripts/iphone/uicontrols.js >> $@
  81 + $(CAT) javascripts/iphone/network.js >> $@
  82 +
80 83 blackberry: lib/blackberry/phonegap-min.js
81 84
82 85 lib/blackberry/phonegap-min.js: lib/blackberry/phonegap.js
23 iphone/Classes/Network.h
... ... @@ -0,0 +1,23 @@
  1 +//
  2 +// Network.h
  3 +// PhoneGap
  4 +//
  5 +// Created by Shazron Abdullah on 29/07/09.
  6 +// Copyright 2009 Nitobi Inc. All rights reserved.
  7 +//
  8 +
  9 +#import <Foundation/Foundation.h>
  10 +#import "PhoneGapCommand.h"
  11 +
  12 +@class Reachability;
  13 +
  14 +@interface Network : PhoneGapCommand {
  15 +
  16 +}
  17 +
  18 +- (void) isReachable:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
  19 +
  20 +- (void) reachabilityChanged:(NSNotification *)note;
  21 +- (void) updateReachability:(NSString*)callback;
  22 +
  23 +@end
64 iphone/Classes/Network.m
... ... @@ -0,0 +1,64 @@
  1 +//
  2 +// Network.m
  3 +// PhoneGap
  4 +//
  5 +// Created by Shazron Abdullah on 29/07/09.
  6 +// Copyright 2009 Nitobi Inc. All rights reserved.
  7 +//
  8 +
  9 +#import "Network.h"
  10 +#import "Reachability.h"
  11 +#import "Categories.h"
  12 +
  13 +@implementation Network
  14 +
  15 +- (void) isReachable:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
  16 +{
  17 + NSUInteger argc = [arguments count];
  18 + NSString* hostName = nil, *callback = nil;
  19 +
  20 + if (argc > 0) hostName = [arguments objectAtIndex:0];
  21 + if (argc > 1) callback = [arguments objectAtIndex:1];
  22 +
  23 + if (argc < 1) {
  24 + NSLog(@"Network.startReachability: Missing 1st argument (hostName).");
  25 + return;
  26 + }
  27 +
  28 + if ([options existsValue:@"true" forKey:@"isIpAddress"]) {
  29 + [[Reachability sharedReachability] setAddress:hostName];
  30 + } else {
  31 + [[Reachability sharedReachability] setHostName:hostName];
  32 + }
  33 +
  34 + //[[Reachability sharedReachability] setNetworkStatusNotificationsEnabled:YES];
  35 + [self updateReachability:callback];
  36 +
  37 + //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:@"kNetworkReachabilityChangedNotification" object:nil];
  38 +}
  39 +
  40 +- (void)reachabilityChanged:(NSNotification *)note
  41 +{
  42 + [self updateReachability:nil];
  43 +}
  44 +
  45 +- (void)updateReachability:(NSString*)callback
  46 +{
  47 + NSString* jsCallback = @"navigator.network.updateReachability";
  48 + if (callback)
  49 + jsCallback = callback;
  50 +
  51 + NSString* status = [[NSString alloc] initWithFormat:@"%@({ hostName: '%@', ipAddress: '%@', remoteHostStatus: %d, internetConnectionStatus: %d, localWiFiConnectionStatus: %d });",
  52 + jsCallback,
  53 + [[Reachability sharedReachability] hostName],
  54 + [[Reachability sharedReachability] address],
  55 + [[Reachability sharedReachability] remoteHostStatus],
  56 + [[Reachability sharedReachability] internetConnectionStatus],
  57 + [[Reachability sharedReachability] localWiFiConnectionStatus]];
  58 +
  59 +
  60 + [webView stringByEvaluatingJavaScriptFromString:status];
  61 + [status release];
  62 +}
  63 +
  64 +@end
122 iphone/Classes/Reachability.h
... ... @@ -0,0 +1,122 @@
  1 +/*
  2 +
  3 +File: Reachability.h
  4 +Abstract: SystemConfiguration framework wrapper.
  5 +
  6 +Version: 1.5
  7 +
  8 +Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
  9 +("Apple") in consideration of your agreement to the following terms, and your
  10 +use, installation, modification or redistribution of this Apple software
  11 +constitutes acceptance of these terms. If you do not agree with these terms,
  12 +please do not use, install, modify or redistribute this Apple software.
  13 +
  14 +In consideration of your agreement to abide by the following terms, and subject
  15 +to these terms, Apple grants you a personal, non-exclusive license, under
  16 +Apple's copyrights in this original Apple software (the "Apple Software"), to
  17 +use, reproduce, modify and redistribute the Apple Software, with or without
  18 +modifications, in source and/or binary forms; provided that if you redistribute
  19 +the Apple Software in its entirety and without modifications, you must retain
  20 +this notice and the following text and disclaimers in all such redistributions
  21 +of the Apple Software.
  22 +Neither the name, trademarks, service marks or logos of Apple Inc. may be used
  23 +to endorse or promote products derived from the Apple Software without specific
  24 +prior written permission from Apple. Except as expressly stated in this notice,
  25 +no other rights or licenses, express or implied, are granted by Apple herein,
  26 +including but not limited to any patent rights that may be infringed by your
  27 +derivative works or by other works in which the Apple Software may be
  28 +incorporated.
  29 +
  30 +The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
  31 +WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  32 +WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  33 +PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  34 +COMBINATION WITH YOUR PRODUCTS.
  35 +
  36 +IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  37 +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  38 +GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39 +ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
  40 +DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
  41 +CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
  42 +APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43 +
  44 +Copyright (C) 2008 Apple Inc. All Rights Reserved.
  45 +
  46 +*/
  47 +
  48 +#import <UIKit/UIKit.h>
  49 +#import <SystemConfiguration/SystemConfiguration.h>
  50 +
  51 +@class Reachability;
  52 +
  53 +@interface Reachability : NSObject {
  54 +
  55 +@private
  56 + BOOL _networkStatusNotificationsEnabled;
  57 +
  58 + NSString *_hostName;
  59 + NSString *_address;
  60 +
  61 + NSMutableDictionary *_reachabilityQueries;
  62 +}
  63 +
  64 +/*
  65 + An enumeration that defines the return values of the network state
  66 + of the device.
  67 + */
  68 +typedef enum {
  69 + NotReachable = 0,
  70 + ReachableViaCarrierDataNetwork,
  71 + ReachableViaWiFiNetwork
  72 +} NetworkStatus;
  73 +
  74 +
  75 +// Set to YES to register for changes in network status. Otherwise reachability queries
  76 +// will be handled synchronously.
  77 +@property BOOL networkStatusNotificationsEnabled;
  78 +// The remote host whose reachability will be queried.
  79 +// Either this or 'addressName' must be set.
  80 +@property (nonatomic, retain) NSString *hostName;
  81 +// The IP address of the remote host whose reachability will be queried.
  82 +// Either this or 'hostName' must be set.
  83 +@property (nonatomic, retain) NSString *address;
  84 +// A cache of ReachabilityQuery objects, which encapsulate a SCNetworkReachabilityRef, a host or address, and a run loop. The keys are host names or addresses.
  85 +@property (nonatomic, assign) NSMutableDictionary *reachabilityQueries;
  86 +
  87 +// This class is intended to be used as a singleton.
  88 ++ (Reachability *)sharedReachability;
  89 +
  90 +// Is self.hostName is not nil, determines its reachability.
  91 +// If self.hostName is nil and self.address is not nil, determines the reachability of self.address.
  92 +- (NetworkStatus)remoteHostStatus;
  93 +// Is the device able to communicate with Internet hosts? If so, through which network interface?
  94 +- (NetworkStatus)internetConnectionStatus;
  95 +// Is the device able to communicate with hosts on the local WiFi network? (Typically these are Bonjour hosts).
  96 +- (NetworkStatus)localWiFiConnectionStatus;
  97 +
  98 +/*
  99 + When reachability change notifications are posted, the callback method 'ReachabilityCallback' is called
  100 + and posts a notification that the client application can observe to learn about changes.
  101 + */
  102 +static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void *info);
  103 +
  104 +@end
  105 +
  106 +@interface ReachabilityQuery : NSObject
  107 +{
  108 +@private
  109 + SCNetworkReachabilityRef _reachabilityRef;
  110 + CFMutableArrayRef _runLoops;
  111 + NSString *_hostNameOrAddress;
  112 +}
  113 +// Keep around each network reachability query object so that we can
  114 +// register for updates from those objects.
  115 +@property (nonatomic) SCNetworkReachabilityRef reachabilityRef;
  116 +@property (nonatomic, retain) NSString *hostNameOrAddress;
  117 +@property (nonatomic) CFMutableArrayRef runLoops;
  118 +
  119 +- (void)scheduleOnRunLoop:(NSRunLoop *)inRunLoop;
  120 +
  121 +@end
  122 +
585 iphone/Classes/Reachability.m
... ... @@ -0,0 +1,585 @@
  1 +/*
  2 +
  3 +File: Reachability.m
  4 +Abstract: SystemConfiguration framework wrapper.
  5 +
  6 +Version: 1.5
  7 +
  8 +Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
  9 +("Apple") in consideration of your agreement to the following terms, and your
  10 +use, installation, modification or redistribution of this Apple software
  11 +constitutes acceptance of these terms. If you do not agree with these terms,
  12 +please do not use, install, modify or redistribute this Apple software.
  13 +
  14 +In consideration of your agreement to abide by the following terms, and subject
  15 +to these terms, Apple grants you a personal, non-exclusive license, under
  16 +Apple's copyrights in this original Apple software (the "Apple Software"), to
  17 +use, reproduce, modify and redistribute the Apple Software, with or without
  18 +modifications, in source and/or binary forms; provided that if you redistribute
  19 +the Apple Software in its entirety and without modifications, you must retain
  20 +this notice and the following text and disclaimers in all such redistributions
  21 +of the Apple Software.
  22 +Neither the name, trademarks, service marks or logos of Apple Inc. may be used
  23 +to endorse or promote products derived from the Apple Software without specific
  24 +prior written permission from Apple. Except as expressly stated in this notice,
  25 +no other rights or licenses, express or implied, are granted by Apple herein,
  26 +including but not limited to any patent rights that may be infringed by your
  27 +derivative works or by other works in which the Apple Software may be
  28 +incorporated.
  29 +
  30 +The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
  31 +WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  32 +WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  33 +PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  34 +COMBINATION WITH YOUR PRODUCTS.
  35 +
  36 +IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  37 +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  38 +GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39 +ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
  40 +DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
  41 +CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
  42 +APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43 +
  44 +Copyright (C) 2008 Apple Inc. All Rights Reserved.
  45 +
  46 +*/
  47 +
  48 +#import <sys/socket.h>
  49 +#import <netinet/in.h>
  50 +#import <netinet6/in6.h>
  51 +#import <arpa/inet.h>
  52 +#import <ifaddrs.h>
  53 +#include <netdb.h>
  54 +
  55 +#import "Reachability.h"
  56 +#import <SystemConfiguration/SCNetworkReachability.h>
  57 +
  58 +static NSString *kLinkLocalAddressKey = @"169.254.0.0";
  59 +static NSString *kDefaultRouteKey = @"0.0.0.0";
  60 +
  61 +static Reachability *_sharedReachability;
  62 +
  63 +// A class extension that declares internal methods for this class.
  64 +@interface Reachability()
  65 +- (BOOL)isAdHocWiFiNetworkAvailableFlags:(SCNetworkReachabilityFlags *)outFlags;
  66 +- (BOOL)isNetworkAvailableFlags:(SCNetworkReachabilityFlags *)outFlags;
  67 +- (BOOL)isReachableWithoutRequiringConnection:(SCNetworkReachabilityFlags)flags;
  68 +- (SCNetworkReachabilityRef)reachabilityRefForHostName:(NSString *)hostName;
  69 +- (SCNetworkReachabilityRef)reachabilityRefForAddress:(NSString *)address;
  70 +- (BOOL)addressFromString:(NSString *)IPAddress address:(struct sockaddr_in *)outAddress;
  71 +- (void)stopListeningForReachabilityChanges;
  72 +@end
  73 +
  74 +@implementation Reachability
  75 +
  76 +@synthesize networkStatusNotificationsEnabled = _networkStatusNotificationsEnabled;
  77 +@synthesize hostName = _hostName;
  78 +@synthesize address = _address;
  79 +@synthesize reachabilityQueries = _reachabilityQueries;
  80 +
  81 ++ (Reachability *)sharedReachability
  82 +{
  83 + if (!_sharedReachability) {
  84 + _sharedReachability = [[Reachability alloc] init];
  85 + // Clients of Reachability will typically call [[Reachability sharedReachability] setHostName:]
  86 + // before calling one of the status methods.
  87 + _sharedReachability.hostName = nil;
  88 + _sharedReachability.address = nil;
  89 + _sharedReachability.networkStatusNotificationsEnabled = NO;
  90 + _sharedReachability.reachabilityQueries = [[NSMutableDictionary alloc] init];
  91 + }
  92 + return _sharedReachability;
  93 +}
  94 +
  95 +- (void) dealloc
  96 +{
  97 + [self stopListeningForReachabilityChanges];
  98 +
  99 + [_sharedReachability.reachabilityQueries release];
  100 + [_sharedReachability release];
  101 + [super dealloc];
  102 +}
  103 +
  104 +- (BOOL)isReachableWithoutRequiringConnection:(SCNetworkReachabilityFlags)flags
  105 +{
  106 + // kSCNetworkReachabilityFlagsReachable indicates that the specified nodename or address can
  107 + // be reached using the current network configuration.
  108 + BOOL isReachable = flags & kSCNetworkReachabilityFlagsReachable;
  109 +
  110 + // This flag indicates that the specified nodename or address can
  111 + // be reached using the current network configuration, but a
  112 + // connection must first be established.
  113 + //
  114 + // If the flag is false, we don't have a connection. But because CFNetwork
  115 + // automatically attempts to bring up a WWAN connection, if the WWAN reachability
  116 + // flag is present, a connection is not required.
  117 + BOOL noConnectionRequired = !(flags & kSCNetworkReachabilityFlagsConnectionRequired);
  118 + if ((flags & kSCNetworkReachabilityFlagsIsWWAN)) {
  119 + noConnectionRequired = YES;
  120 + }
  121 +
  122 + return (isReachable && noConnectionRequired) ? YES : NO;
  123 +}
  124 +
  125 +// Returns whether or not the current host name is reachable with the current network configuration.
  126 +- (BOOL)isHostReachable:(NSString *)host
  127 +{
  128 + if (!host || ![host length]) {
  129 + return NO;
  130 + }
  131 +
  132 + SCNetworkReachabilityFlags flags;
  133 + SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, [host UTF8String]);
  134 + BOOL gotFlags = SCNetworkReachabilityGetFlags(reachability, &flags);
  135 +
  136 + CFRelease(reachability);
  137 +
  138 + if (!gotFlags) {
  139 + return NO;
  140 + }
  141 +
  142 + return [self isReachableWithoutRequiringConnection:flags];
  143 +}
  144 +
  145 +// This returns YES if the address 169.254.0.0 is reachable without requiring a connection.
  146 +- (BOOL)isAdHocWiFiNetworkAvailableFlags:(SCNetworkReachabilityFlags *)outFlags
  147 +{
  148 + // Look in the cache of reachability queries for one that matches this query.
  149 + ReachabilityQuery *query = [self.reachabilityQueries objectForKey:kLinkLocalAddressKey];
  150 + SCNetworkReachabilityRef adHocWiFiNetworkReachability = query.reachabilityRef;
  151 +
  152 + // If a cached reachability query was not found, create one.
  153 + if (!adHocWiFiNetworkReachability) {
  154 +
  155 + // Build a sockaddr_in that we can pass to the address reachability query.
  156 + struct sockaddr_in sin;
  157 +
  158 + bzero(&sin, sizeof(sin));
  159 + sin.sin_len = sizeof(sin);
  160 + sin.sin_family = AF_INET;
  161 + // IN_LINKLOCALNETNUM is defined in <netinet/in.h> as 169.254.0.0
  162 + sin.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM);
  163 +
  164 + adHocWiFiNetworkReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&sin);
  165 +
  166 + query = [[[ReachabilityQuery alloc] init] autorelease];
  167 + query.hostNameOrAddress = kLinkLocalAddressKey;
  168 + query.reachabilityRef = adHocWiFiNetworkReachability;
  169 +
  170 + // Add the reachability query to the cache.
  171 + [self.reachabilityQueries setObject:query forKey:kLinkLocalAddressKey];
  172 + }
  173 +
  174 + // If necessary, register for notifcations for the SCNetworkReachabilityRef on the current run loop.
  175 + // If an existing SCNetworkReachabilityRef was found in the cache, we can reuse it and register
  176 + // to receive notifications from it in the current run loop, which may be different than the run loop
  177 + // that was previously used when registering the SCNetworkReachabilityRef for notifications.
  178 + // -scheduleOnRunLoop: will schedule only if network status notifications are enabled in the Reachability instance.
  179 + // By default, they are not enabled.
  180 + [query scheduleOnRunLoop:[NSRunLoop currentRunLoop]];
  181 +
  182 + SCNetworkReachabilityFlags addressReachabilityFlags;
  183 + BOOL gotFlags = SCNetworkReachabilityGetFlags(adHocWiFiNetworkReachability, &addressReachabilityFlags);
  184 + if (!gotFlags) {
  185 + // There was an error getting the reachability flags.
  186 + return NO;
  187 + }
  188 +
  189 + // Callers of this method might want to use the reachability flags, so if an 'out' parameter
  190 + // was passed in, assign the reachability flags to it.
  191 + if (outFlags) {
  192 + *outFlags = addressReachabilityFlags;
  193 + }
  194 +
  195 + return [self isReachableWithoutRequiringConnection:addressReachabilityFlags];
  196 +}
  197 +
  198 +// ReachabilityCallback is registered as the callback for network state changes in startListeningForReachabilityChanges.
  199 +static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void *info)
  200 +{
  201 + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  202 +
  203 + // Post a notification to notify the client that the network reachability changed.
  204 + [[NSNotificationCenter defaultCenter] postNotificationName:@"kNetworkReachabilityChangedNotification" object:nil];
  205 +
  206 + [pool release];
  207 +}
  208 +
  209 +// Perform a reachability query for the address 0.0.0.0. If that address is reachable without
  210 +// requiring a connection, a network interface is available. We'll have to do more work to
  211 +// determine which network interface is available.
  212 +- (BOOL)isNetworkAvailableFlags:(SCNetworkReachabilityFlags *)outFlags
  213 +{
  214 + ReachabilityQuery *query = [self.reachabilityQueries objectForKey:kDefaultRouteKey];
  215 + SCNetworkReachabilityRef defaultRouteReachability = query.reachabilityRef;
  216 +
  217 + // If a cached reachability query was not found, create one.
  218 + if (!defaultRouteReachability) {
  219 +
  220 + struct sockaddr_in zeroAddress;
  221 + bzero(&zeroAddress, sizeof(zeroAddress));
  222 + zeroAddress.sin_len = sizeof(zeroAddress);
  223 + zeroAddress.sin_family = AF_INET;
  224 +
  225 + defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
  226 +
  227 + ReachabilityQuery *query = [[[ReachabilityQuery alloc] init] autorelease];
  228 + query.hostNameOrAddress = kDefaultRouteKey;
  229 + query.reachabilityRef = defaultRouteReachability;
  230 +
  231 + [self.reachabilityQueries setObject:query forKey:kDefaultRouteKey];
  232 + }
  233 +
  234 + // If necessary, register for notifcations for the SCNetworkReachabilityRef on the current run loop.
  235 + // If an existing SCNetworkReachabilityRef was found in the cache, we can reuse it and register
  236 + // to receive notifications from it in the current run loop, which may be different than the run loop
  237 + // that was previously used when registering the SCNetworkReachabilityRef for notifications.
  238 + // -scheduleOnRunLoop: will schedule only if network status notifications are enabled in the Reachability instance.
  239 + // By default, they are not enabled.
  240 + [query scheduleOnRunLoop:[NSRunLoop currentRunLoop]];
  241 +
  242 + SCNetworkReachabilityFlags flags;
  243 + BOOL gotFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
  244 + if (!gotFlags) {
  245 + return NO;
  246 + }
  247 +
  248 + BOOL isReachable = [self isReachableWithoutRequiringConnection:flags];
  249 +
  250 + // Callers of this method might want to use the reachability flags, so if an 'out' parameter
  251 + // was passed in, assign the reachability flags to it.
  252 + if (outFlags) {
  253 + *outFlags = flags;
  254 + }
  255 +
  256 + return isReachable;
  257 +}
  258 +
  259 +// Be a good citizen and unregister for network state changes when the application terminates.
  260 +- (void)stopListeningForReachabilityChanges
  261 +{
  262 + // Walk through the cache that holds SCNetworkReachabilityRefs for reachability
  263 + // queries to particular hosts or addresses.
  264 + NSEnumerator *enumerator = [self.reachabilityQueries objectEnumerator];
  265 + ReachabilityQuery *reachabilityQuery;
  266 +
  267 + while (reachabilityQuery = [enumerator nextObject]) {
  268 +
  269 + CFArrayRef runLoops = reachabilityQuery.runLoops;
  270 + NSUInteger runLoopCounter, maxRunLoops = CFArrayGetCount(runLoops);
  271 +
  272 + for (runLoopCounter = 0; runLoopCounter < maxRunLoops; runLoopCounter++) {
  273 + CFRunLoopRef nextRunLoop = (CFRunLoopRef)CFArrayGetValueAtIndex(runLoops, runLoopCounter);
  274 +
  275 + SCNetworkReachabilityUnscheduleFromRunLoop(reachabilityQuery.reachabilityRef, nextRunLoop, kCFRunLoopDefaultMode);
  276 + }
  277 +
  278 + CFArrayRemoveAllValues(reachabilityQuery.runLoops);
  279 + }
  280 +}
  281 +
  282 +/*
  283 + Create a SCNetworkReachabilityRef for hostName, which lets us determine if hostName
  284 + is currently reachable, and lets us register to receive notifications when the
  285 + reachability of hostName changes.
  286 + */
  287 +- (SCNetworkReachabilityRef)reachabilityRefForHostName:(NSString *)hostName
  288 +{
  289 + if (!hostName || ![hostName length]) {
  290 + return NULL;
  291 + }
  292 +
  293 + // Look in the cache for an existing SCNetworkReachabilityRef for hostName.
  294 + ReachabilityQuery *cachedQuery = [self.reachabilityQueries objectForKey:hostName];
  295 + SCNetworkReachabilityRef reachabilityRefForHostName = cachedQuery.reachabilityRef;
  296 +
  297 + if (reachabilityRefForHostName) {
  298 + return reachabilityRefForHostName;
  299 + }
  300 +
  301 + // Didn't find an existing SCNetworkReachabilityRef for hostName, so create one ...
  302 + reachabilityRefForHostName = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [hostName UTF8String]);
  303 +
  304 + NSAssert1(reachabilityRefForHostName != NULL, @"Failed to create SCNetworkReachabilityRef for host: %@", hostName);
  305 +
  306 + ReachabilityQuery *query = [[[ReachabilityQuery alloc] init] autorelease];
  307 + query.hostNameOrAddress = hostName;
  308 + query.reachabilityRef = reachabilityRefForHostName;
  309 +
  310 + // If necessary, register for notifcations for the SCNetworkReachabilityRef on the current run loop.
  311 + // If an existing SCNetworkReachabilityRef was found in the cache, we can reuse it and register
  312 + // to receive notifications from it in the current run loop, which may be different than the run loop
  313 + // that was previously used when registering the SCNetworkReachabilityRef for notifications.
  314 + // -scheduleOnRunLoop: will schedule only if network status notifications are enabled in the Reachability instance.
  315 + // By default, they are not enabled.
  316 + [query scheduleOnRunLoop:[NSRunLoop currentRunLoop]];
  317 +
  318 + // ... and add it to the cache.
  319 + [self.reachabilityQueries setObject:query forKey:hostName];
  320 + return reachabilityRefForHostName;
  321 +}
  322 +
  323 +/*
  324 + Create a SCNetworkReachabilityRef for the IP address in addressString, which lets us determine if
  325 + the address is currently reachable, and lets us register to receive notifications when the
  326 + reachability of the address changes.
  327 + */
  328 +- (SCNetworkReachabilityRef)reachabilityRefForAddress:(NSString *)addressString
  329 +{
  330 + if (!addressString || ![addressString length]) {
  331 + return NULL;
  332 + }
  333 +
  334 + struct sockaddr_in address;
  335 +
  336 + BOOL gotAddress = [self addressFromString:addressString address:&address];
  337 + if (!gotAddress) {
  338 + // The attempt to convert addressString to a sockaddr_in failed.
  339 + NSAssert1(gotAddress != NO, @"Failed to convert an IP address string to a sockaddr_in: %@", addressString);
  340 + return NULL;
  341 + }
  342 +
  343 + // Look in the cache for an existing SCNetworkReachabilityRef for addressString.
  344 + ReachabilityQuery *cachedQuery = [self.reachabilityQueries objectForKey:addressString];
  345 + SCNetworkReachabilityRef reachabilityRefForAddress = cachedQuery.reachabilityRef;
  346 +
  347 + if (reachabilityRefForAddress) {
  348 + return reachabilityRefForAddress;
  349 + }
  350 +
  351 + // Didn't find an existing SCNetworkReachabilityRef for addressString, so create one.
  352 + reachabilityRefForAddress = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (struct sockaddr *)&address);
  353 +
  354 + NSAssert1(reachabilityRefForAddress != NULL, @"Failed to create SCNetworkReachabilityRef for address: %@", addressString);
  355 +
  356 + ReachabilityQuery *query = [[[ReachabilityQuery alloc] init] autorelease];
  357 + query.hostNameOrAddress = addressString;
  358 + query.reachabilityRef = reachabilityRefForAddress;
  359 +
  360 + // If necessary, register for notifcations for the SCNetworkReachabilityRef on the current run loop.
  361 + // If an existing SCNetworkReachabilityRef was found in the cache, we can reuse it and register
  362 + // to receive notifications from it in the current run loop, which may be different than the run loop
  363 + // that was previously used when registering the SCNetworkReachabilityRef for notifications.
  364 + // -scheduleOnRunLoop: will schedule only if network status notifications are enabled in the Reachability instance.
  365 + // By default, they are not enabled.
  366 + [query scheduleOnRunLoop:[NSRunLoop currentRunLoop]];
  367 +
  368 + // ... and add it to the cache.
  369 + [self.reachabilityQueries setObject:query forKey:addressString];
  370 + return reachabilityRefForAddress;
  371 +}
  372 +
  373 +- (NetworkStatus)remoteHostStatus
  374 +{
  375 + /*
  376 + If the current host name or address is reachable, determine which network interface it is reachable through.
  377 + If the host is reachable and the reachability flags include kSCNetworkReachabilityFlagsIsWWAN, it
  378 + is reachable through the carrier data network. If the host is reachable and the reachability
  379 + flags do not include kSCNetworkReachabilityFlagsIsWWAN, it is reachable through the WiFi network.
  380 + */
  381 +
  382 + SCNetworkReachabilityRef reachabilityRef = nil;
  383 + if (self.hostName) {
  384 + reachabilityRef = [self reachabilityRefForHostName:self.hostName];
  385 +
  386 + } else if (self.address) {
  387 + reachabilityRef = [self reachabilityRefForAddress:self.address];
  388 +
  389 + } else {
  390 + NSAssert(self.hostName != nil && self.address != nil, @"No hostName or address specified. Cannot determine reachability.");
  391 + return NotReachable;
  392 + }
  393 +
  394 + if (!reachabilityRef) {
  395 + return NotReachable;
  396 + }
  397 +
  398 + SCNetworkReachabilityFlags reachabilityFlags;
  399 + BOOL gotFlags = SCNetworkReachabilityGetFlags(reachabilityRef, &reachabilityFlags);
  400 + if (!gotFlags) {
  401 + return NotReachable;
  402 + }
  403 +
  404 + BOOL reachable = [self isReachableWithoutRequiringConnection:reachabilityFlags];
  405 +
  406 + if (!reachable) {
  407 + return NotReachable;
  408 + }
  409 + if (reachabilityFlags & ReachableViaCarrierDataNetwork) {
  410 + return ReachableViaCarrierDataNetwork;
  411 + }
  412 +
  413 + return ReachableViaWiFiNetwork;
  414 +}
  415 +
  416 +- (NetworkStatus)internetConnectionStatus
  417 +{
  418 + /*
  419 + To determine if the device has an Internet connection, query the address
  420 + 0.0.0.0. If it's reachable without requiring a connection, first check
  421 + for the kSCNetworkReachabilityFlagsIsDirect flag, which tell us if the connection
  422 + is to an ad-hoc WiFi network. If it is not, the device can access the Internet.
  423 + The next thing to determine is how the device can access the Internet, which
  424 + can either be through the carrier data network (EDGE or other service) or through
  425 + a WiFi connection.
  426 +
  427 + Note: Knowing that the device has an Internet connection is not the same as
  428 + knowing if the device can reach a particular host. To know that, use
  429 + -[Reachability remoteHostStatus].
  430 + */
  431 +
  432 + SCNetworkReachabilityFlags defaultRouteFlags;
  433 + BOOL defaultRouteIsAvailable = [self isNetworkAvailableFlags:&defaultRouteFlags];
  434 + if (defaultRouteIsAvailable) {
  435 +
  436 + if (defaultRouteFlags & kSCNetworkReachabilityFlagsIsDirect) {
  437 +
  438 + // The connection is to an ad-hoc WiFi network, so Internet access is not available.
  439 + return NotReachable;
  440 + }
  441 + else if (defaultRouteFlags & ReachableViaCarrierDataNetwork) {
  442 + return ReachableViaCarrierDataNetwork;
  443 + }
  444 +
  445 + return ReachableViaWiFiNetwork;
  446 + }
  447 +
  448 + return NotReachable;
  449 +}
  450 +
  451 +- (NetworkStatus)localWiFiConnectionStatus
  452 +{
  453 + SCNetworkReachabilityFlags selfAssignedAddressFlags;
  454 +
  455 + /*
  456 + To determine if the WiFi connection is to a local ad-hoc network,
  457 + check the availability of the address 169.254.x.x. That's an address
  458 + in the self-assigned range, and the device will have a self-assigned IP
  459 + when it's connected to a ad-hoc WiFi network. So to test if the device
  460 + has a self-assigned IP, look for the kSCNetworkReachabilityFlagsIsDirect flag
  461 + in the address query. If it's present, we know that the WiFi connection
  462 + is to an ad-hoc network.
  463 + */
  464 + // This returns YES if the address 169.254.0.0 is reachable without requiring a connection.
  465 + BOOL hasLinkLocalNetworkAccess = [self isAdHocWiFiNetworkAvailableFlags:&selfAssignedAddressFlags];
  466 +
  467 + if (hasLinkLocalNetworkAccess && (selfAssignedAddressFlags & kSCNetworkReachabilityFlagsIsDirect)) {
  468 + return ReachableViaWiFiNetwork;
  469 + }
  470 +
  471 + return NotReachable;
  472 +}
  473 +
  474 +// Convert an IP address from an NSString to a sockaddr_in * that can be used to create
  475 +// the reachability request.
  476 +- (BOOL)addressFromString:(NSString *)IPAddress address:(struct sockaddr_in *)address
  477 +{
  478 + if (!IPAddress || ![IPAddress length]) {
  479 + return NO;
  480 + }
  481 +
  482 + memset((char *) address, sizeof(struct sockaddr_in), 0);
  483 + address->sin_family = AF_INET;
  484 + address->sin_len = sizeof(struct sockaddr_in);
  485 +
  486 + int conversionResult = inet_aton([IPAddress UTF8String], &address->sin_addr);
  487 + if (conversionResult == 0) {
  488 + NSAssert1(conversionResult != 1, @"Failed to convert the IP address string into a sockaddr_in: %@", IPAddress);
  489 + return NO;
  490 + }
  491 +
  492 + return YES;
  493 +}
  494 +
  495 +@end
  496 +
  497 +@interface ReachabilityQuery ()
  498 +- (CFRunLoopRef)startListeningForReachabilityChanges:(SCNetworkReachabilityRef)reachability onRunLoop:(CFRunLoopRef)runLoop;
  499 +@end
  500 +
  501 +@implementation ReachabilityQuery
  502 +
  503 +@synthesize reachabilityRef = _reachabilityRef;
  504 +@synthesize runLoops = _runLoops;
  505 +@synthesize hostNameOrAddress = _hostNameOrAddress;
  506 +
  507 +- (id)init
  508 +{
  509 + self = [super init];
  510 + if (self != nil) {
  511 + self.runLoops = CFArrayCreateMutable(kCFAllocatorDefault, 0, NULL);
  512 + }
  513 + return self;
  514 +}
  515 +
  516 +- (void)dealloc
  517 +{
  518 + CFRelease(self.runLoops);
  519 + [super dealloc];
  520 +}
  521 +
  522 +- (BOOL)isScheduledOnRunLoop:(CFRunLoopRef)runLoop
  523 +{
  524 + NSUInteger runLoopCounter, maxRunLoops = CFArrayGetCount(self.runLoops);
  525 +
  526 + for (runLoopCounter = 0; runLoopCounter < maxRunLoops; runLoopCounter++) {
  527 + CFRunLoopRef nextRunLoop = (CFRunLoopRef)CFArrayGetValueAtIndex(self.runLoops, runLoopCounter);
  528 +
  529 + if (nextRunLoop == runLoop) {
  530 + return YES;
  531 + }
  532 + }
  533 +
  534 + return NO;
  535 +}
  536 +
  537 +- (void)scheduleOnRunLoop:(NSRunLoop *)inRunLoop
  538 +{
  539 + // Only register for network state changes if the client has specifically enabled them.
  540 + if ([[Reachability sharedReachability] networkStatusNotificationsEnabled] == NO) {
  541 + return;
  542 + }
  543 +
  544 + if (!inRunLoop) {
  545 + return;
  546 + }
  547 +
  548 + CFRunLoopRef runLoop = [inRunLoop getCFRunLoop];
  549 +
  550 + // Notifications of status changes for each reachability query can be scheduled on multiple run loops.
  551 + // To support that, register for notifications for each runLoop.
  552 + // -isScheduledOnRunLoop: iterates over all of the run loops that have previously been used
  553 + // to register for notifications. If one is found that matches the passed in runLoop argument, there's
  554 + // no need to register for notifications again. If one is not found, register for notifications
  555 + // using the current runLoop.
  556 + if (![self isScheduledOnRunLoop:runLoop]) {
  557 +
  558 + CFRunLoopRef notificationRunLoop = [self startListeningForReachabilityChanges:self.reachabilityRef onRunLoop:runLoop];
  559 + if (notificationRunLoop) {
  560 + CFArrayAppendValue(self.runLoops, notificationRunLoop);
  561 + }
  562 + }
  563 +}
  564 +
  565 +// Register to receive changes to the 'reachability' query so that we can update the
  566 +// user interface when the network state changes.
  567 +- (CFRunLoopRef)startListeningForReachabilityChanges:(SCNetworkReachabilityRef)reachability onRunLoop:(CFRunLoopRef)runLoop
  568 +{
  569 + if (!reachability) {
  570 + return NULL;
  571 + }
  572 +
  573 + if (!runLoop) {
  574 + return NULL;
  575 + }
  576 +
  577 + SCNetworkReachabilityContext context = {0, self, NULL, NULL, NULL};
  578 + SCNetworkReachabilitySetCallback(reachability, ReachabilityCallback, &context);
  579 + SCNetworkReachabilityScheduleWithRunLoop(reachability, runLoop, kCFRunLoopDefaultMode);
  580 +
  581 + return runLoop;
  582 +}
  583 +
  584 +
  585 +@end
16 iphone/PhoneGap.xcodeproj/project.pbxproj
@@ -20,6 +20,9 @@
20 20 28AD733F0D9D9553002E5188 /* MainWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 28AD733E0D9D9553002E5188 /* MainWindow.xib */; };
21 21 303554C1100E56B7002C5EC0 /* Camera.m in Sources */ = {isa = PBXBuildFile; fileRef = 303554C0100E56B7002C5EC0 /* Camera.m */; };
22 22 30355582100E7477002C5EC0 /* NSData+Base64.m in Sources */ = {isa = PBXBuildFile; fileRef = 30355581100E7477002C5EC0 /* NSData+Base64.m */; };
  23 + 30C531981020E3B20017260B /* Reachability.m in Sources */ = {isa = PBXBuildFile; fileRef = 30C531971020E3B20017260B /* Reachability.m */; };
  24 + 30C5319B1020E3CF0017260B /* Network.m in Sources */ = {isa = PBXBuildFile; fileRef = 30C5319A1020E3CF0017260B /* Network.m */; };
  25 + 30C531A01020E4310017260B /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 30C5319F1020E4310017260B /* SystemConfiguration.framework */; };
23 26 30CD0F0F0FCBED3F00AA6748 /* AddressBookUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3023AB3D0FCB6FC40012AC33 /* AddressBookUI.framework */; };
24 27 30EC9AEC0FCDF4DD00A52C71 /* LoadingView.m in Sources */ = {isa = PBXBuildFile; fileRef = 30EC9AEB0FCDF4DD00A52C71 /* LoadingView.m */; };
25 28 30EC9B110FCDFC9900A52C71 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 30EC9B100FCDFC9900A52C71 /* CoreGraphics.framework */; };
@@ -70,6 +73,11 @@
70 73 303554C0100E56B7002C5EC0 /* Camera.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Camera.m; sourceTree = "<group>"; };
71 74 30355580100E7477002C5EC0 /* NSData+Base64.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSData+Base64.h"; sourceTree = "<group>"; };
72 75 30355581100E7477002C5EC0 /* NSData+Base64.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSData+Base64.m"; sourceTree = "<group>"; };
  76 + 30C531961020E3B20017260B /* Reachability.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Reachability.h; sourceTree = "<group>"; };
  77 + 30C531971020E3B20017260B /* Reachability.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Reachability.m; sourceTree = "<group>"; };
  78 + 30C531991020E3CF0017260B /* Network.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Network.h; sourceTree = "<group>"; };
  79 + 30C5319A1020E3CF0017260B /* Network.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Network.m; sourceTree = "<group>"; };
  80 + 30C5319F1020E4310017260B /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; };
73 81 30EC9AEA0FCDF4DD00A52C71 /* LoadingView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LoadingView.h; sourceTree = "<group>"; };
74 82 30EC9AEB0FCDF4DD00A52C71 /* LoadingView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LoadingView.m; sourceTree = "<group>"; };
75 83 30EC9B100FCDFC9900A52C71 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
@@ -125,6 +133,7 @@
125 133 BB8FC69F0F5BB58D007C7DCE /* CFNetwork.framework in Frameworks */,
126 134 30EC9B110FCDFC9900A52C71 /* CoreGraphics.framework in Frameworks */,
127 135 30EC9B220FCDFCC500A52C71 /* QuartzCore.framework in Frameworks */,
  136 + 30C531A01020E4310017260B /* SystemConfiguration.framework in Frameworks */,
128 137 );
129 138 runOnlyForDeploymentPostprocessing = 0;
130 139 };
@@ -162,6 +171,8 @@
162 171 1B1B50FB0F95000A00ECD4E6 /* Commands */ = {
163 172 isa = PBXGroup;
164 173 children = (
  174 + 30C531961020E3B20017260B /* Reachability.h */,
  175 + 30C531971020E3B20017260B /* Reachability.m */,
165 176 30355580100E7477002C5EC0 /* NSData+Base64.h */,
166 177 30355581100E7477002C5EC0 /* NSData+Base64.m */,
167 178 BB8FC5C10F5B3EC4007C7DCE /* Bonjour.h */,
@@ -186,6 +197,8 @@
186 197 1B1B4E6A0F938A0500ECD4E6 /* UIControls.m */,
187 198 303554BF100E56B7002C5EC0 /* Camera.h */,
188 199 303554C0100E56B7002C5EC0 /* Camera.m */,
  200 + 30C531991020E3CF0017260B /* Network.h */,
  201 + 30C5319A1020E3CF0017260B /* Network.m */,
189 202 );
190 203 name = Commands;
191 204 sourceTree = "<group>";
@@ -227,6 +240,7 @@
227 240 29B97323FDCFA39411CA2CEA /* Frameworks */ = {
228 241 isa = PBXGroup;
229 242 children = (
  243 + 30C5319F1020E4310017260B /* SystemConfiguration.framework */,
230 244 30EC9B210FCDFCC500A52C71 /* QuartzCore.framework */,
231 245 30EC9B100FCDFC9900A52C71 /* CoreGraphics.framework */,
232 246 079B0A090E45734600755F37 /* CoreLocation.framework */,
@@ -361,6 +375,8 @@
361 375 30EF08140FCFC8E0009B3930 /* OCABMutableMultiValue.m in Sources */,
362 376 303554C1100E56B7002C5EC0 /* Camera.m in Sources */,
363 377 30355582100E7477002C5EC0 /* NSData+Base64.m in Sources */,
  378 + 30C531981020E3B20017260B /* Reachability.m in Sources */,
  379 + 30C5319B1020E3CF0017260B /* Network.m in Sources */,
364 380 );
365 381 runOnlyForDeploymentPostprocessing = 0;
366 382 };
20 javascripts/iphone/network.js
... ... @@ -0,0 +1,20 @@
  1 +// Gets the function name of a Function object, else uses "alert" if anonymous
  2 +function GetFunctionName(fn)
  3 +{
  4 + if (fn) {
  5 + var m = fn.toString().match(/^\s*function\s+([^\s\(]+)/);
  6 + return m ? m[1] : "alert";
  7 + } else {
  8 + return null;
  9 + }
  10 +}
  11 +
  12 +/**
  13 + *
  14 + * @param {Function} successCallback
  15 + * @param {Function} errorCallback
  16 + * @param {Object} options (isIpAddress:boolean)
  17 + */
  18 +Network.prototype.isReachable = function(hostName, successCallback, options) {
  19 + PhoneGap.exec("Network.isReachable", hostName, GetFunctionName(successCallback), options);
  20 +}
46 javascripts/network.js
... ... @@ -0,0 +1,46 @@
  1 +/**
  2 + * This class contains information about any NetworkStatus.
  3 + * @constructor
  4 + */
  5 +function NetworkStatus() {
  6 + this.code = null;
  7 + this.message = "";
  8 +}
  9 +
  10 +NetworkStatus.NOT_REACHABLE = 0;
  11 +NetworkStatus.REACHABLE_VIA_CARRIER_DATA_NETWORK = 1;
  12 +NetworkStatus.REACHABLE_VIA_WIFI_NETWORK = 2;
  13 +
  14 +/**
  15 + * This class provides access to device Network data (reachability).
  16 + * @constructor
  17 + */
  18 +function Network() {
  19 + /**
  20 + * The last known Network status.
  21 + * { hostName: string, ipAddress: string,
  22 + remoteHostStatus: int(0/1/2), internetConnectionStatus: int(0/1/2), localWiFiConnectionStatus: int (0/2) }
  23 + */
  24 + this.lastReachability = null;
  25 +};
  26 +
  27 +/**
  28 + *
  29 + * @param {Function} successCallback
  30 + * @param {Function} errorCallback
  31 + * @param {Object} options (isIpAddress:boolean)
  32 + */
  33 +Network.prototype.isReachable = function(hostName, successCallback, options) {
  34 +}
  35 +
  36 +/**
  37 + * Called by the geolocation framework when the reachability status has changed.
  38 + * @param {Reachibility} reachability The current reachability status.
  39 + */
  40 +Network.prototype.updateReachability = function(reachability) {
  41 + this.lastReachability = reachability;
  42 +};
  43 +
  44 +PhoneGap.addConstructor(function() {
  45 + if (typeof navigator.network == "undefined") navigator.network = new Network();
  46 +});

0 comments on commit 23e3fed

Please sign in to comment.
Something went wrong with that request. Please try again.