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 <agri...@chromium.org>
Authored: Wed Oct 10 15:51:57 2012 -0400
Committer: Andrew Grieve <agri...@chromium.org>
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

Reply via email to