Return-Path: X-Original-To: apmail-incubator-callback-commits-archive@minotaur.apache.org Delivered-To: apmail-incubator-callback-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 5EE3B9F96 for ; Wed, 10 Oct 2012 20:01:51 +0000 (UTC) Received: (qmail 90815 invoked by uid 500); 10 Oct 2012 20:01:51 -0000 Delivered-To: apmail-incubator-callback-commits-archive@incubator.apache.org Received: (qmail 90788 invoked by uid 500); 10 Oct 2012 20:01:51 -0000 Mailing-List: contact callback-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: callback-dev@incubator.apache.org Delivered-To: mailing list callback-commits@incubator.apache.org Received: (qmail 90780 invoked by uid 99); 10 Oct 2012 20:01:51 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 10 Oct 2012 20:01:51 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id EB45F3D5CB; Wed, 10 Oct 2012 20:01:50 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: agrieve@apache.org To: callback-commits@incubator.apache.org X-Mailer: ASF-Git Admin Mailer Subject: ios commit: CDVConnection: Use multiple callback results instead of evalling JS Message-Id: <20121010200150.EB45F3D5CB@tyr.zones.apache.org> Date: Wed, 10 Oct 2012 20:01:50 +0000 (UTC) Updated Branches: refs/heads/master ea5d5ec05 -> ffdc81be5 CDVConnection: Use multiple callback results instead of evalling JS This fixes https://issues.apache.org/jira/browse/CB-1604 This also fixes a bug where it was returning network status "unknown" instead of "none" (switch statement was wrong). Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/commit/ffdc81be Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/tree/ffdc81be Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/diff/ffdc81be Branch: refs/heads/master Commit: ffdc81be5a9e462aa909ccd416427a4e2578a8ef Parents: ea5d5ec Author: Andrew Grieve Authored: Wed Oct 10 15:51:57 2012 -0400 Committer: Andrew Grieve Committed: Wed Oct 10 15:51:57 2012 -0400 ---------------------------------------------------------------------- CordovaLib/Classes/CDVConnection.h | 1 + CordovaLib/Classes/CDVConnection.m | 68 ++++++++---------------------- 2 files changed, 19 insertions(+), 50 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/ffdc81be/CordovaLib/Classes/CDVConnection.h ---------------------------------------------------------------------- diff --git a/CordovaLib/Classes/CDVConnection.h b/CordovaLib/Classes/CDVConnection.h index b39579a..d3e8c5d 100644 --- a/CordovaLib/Classes/CDVConnection.h +++ b/CordovaLib/Classes/CDVConnection.h @@ -23,6 +23,7 @@ @interface CDVConnection : CDVPlugin { NSString* type; + NSString* _callbackId; CDVReachability* internetReach; } http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/ffdc81be/CordovaLib/Classes/CDVConnection.m ---------------------------------------------------------------------- diff --git a/CordovaLib/Classes/CDVConnection.m b/CordovaLib/Classes/CDVConnection.m index 3eef1d5..3030711 100644 --- a/CordovaLib/Classes/CDVConnection.m +++ b/CordovaLib/Classes/CDVConnection.m @@ -22,6 +22,7 @@ @interface CDVConnection (PrivateMethods) - (void)updateOnlineStatus; +- (void)sendPluginResult; @end @implementation CDVConnection @@ -30,9 +31,16 @@ - (void)getConnectionInfo:(CDVInvokedUrlCommand*)command { + _callbackId = command.callbackId; + [self sendPluginResult]; +} + +- (void)sendPluginResult +{ CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:self.connectionType]; - [self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; + [result setKeepCallbackAsBool:YES]; + [self.commandDelegate sendPluginResult:result callbackId:_callbackId]; } - (NSString*)w3cConnectionTypeFor:(CDVReachability*)reachability @@ -41,7 +49,7 @@ switch (networkStatus) { case NotReachable: - return @"unknown"; + return @"none"; case ReachableViaWWAN: return @"2g"; // no generic default, so we use the lowest common denominator @@ -50,7 +58,7 @@ return @"wifi"; default: - return @"none"; + return @"unknown"; } } @@ -72,14 +80,7 @@ self.connectionType = [self w3cConnectionTypeFor:reachability]; } } - - NSString* js = nil; - // write the connection type - js = [NSString stringWithFormat:@"navigator.network.connection.type = '%@';", self.connectionType]; - [self.commandDelegate evalJs:js]; - - // send "online"/"offline" event - [self updateOnlineStatus]; + [self sendPluginResult]; } - (void)updateConnectionType:(NSNotification*)note @@ -91,31 +92,6 @@ } } -- (void)updateOnlineStatus -{ - // send "online"/"offline" event - NetworkStatus status = [self.internetReach currentReachabilityStatus]; - BOOL online = (status == ReachableViaWiFi) || (status == ReachableViaWWAN); - - if (online) { - [self.commandDelegate evalJs:@"cordova.fireDocumentEvent('online');"]; - } else { - [self.commandDelegate evalJs:@"cordova.fireDocumentEvent('offline');"]; - } -} - -- (void)prepare -{ - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateConnectionType:) - name:kReachabilityChangedNotification object:nil]; - - self.internetReach = [CDVReachability reachabilityForInternetConnection]; - [self.internetReach startNotifier]; - self.connectionType = [self w3cConnectionTypeFor:self.internetReach]; - - [self performSelector:@selector(updateOnlineStatus) withObject:nil afterDelay:1.0]; -} - - (void)onPause { [self.internetReach stopNotifier]; @@ -129,10 +105,14 @@ - (CDVPlugin*)initWithWebView:(UIWebView*)theWebView { - self = (CDVConnection*)[super initWithWebView:theWebView]; + self = [super initWithWebView:theWebView]; if (self) { self.connectionType = @"none"; - [self prepare]; + self.internetReach = [CDVReachability reachabilityForInternetConnection]; + self.connectionType = [self w3cConnectionTypeFor:self.internetReach]; + [self.internetReach startNotifier]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateConnectionType:) + name:kReachabilityChangedNotification object:nil]; if (&UIApplicationDidEnterBackgroundNotification && &UIApplicationWillEnterForegroundNotification) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onPause) name:UIApplicationDidEnterBackgroundNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onResume) name:UIApplicationWillEnterForegroundNotification object:nil]; @@ -141,16 +121,4 @@ return self; } -- (void)dealloc -{ - [self onReset]; - [[NSNotificationCenter defaultCenter] removeObserver:self]; // this will remove all notifications unless added using addObserverForName:object:queue:usingBlock: -} - -- (void)onReset -{ - // Update the value cached in Javascript after a reset, because it would have been lost on navigation. - [self performSelector:@selector(updateOnlineStatus) withObject:nil afterDelay:1.0]; -} - @end