You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by mm...@apache.org on 2013/02/25 14:41:00 UTC
[48/50] ios commit: [ios] CB-2239: Support multiple plugin result
values.
[ios] CB-2239: Support multiple plugin result values.
This allows a plugin to return multiple cordova exec bridge supported
plugin results. The difference between a multipart message result and a
normal NSArray, is that a multipart message is "massaged" across the
bridge while an NSArray is just sent as-is. This means that a multipart
message can include ArrayBuffers, while a normal NSArray plugin result
will not be scanned at all (possibly better perf).
Project: http://git-wip-us.apache.org/repos/asf/cordova-ios/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-ios/commit/6bb1a85b
Tree: http://git-wip-us.apache.org/repos/asf/cordova-ios/tree/6bb1a85b
Diff: http://git-wip-us.apache.org/repos/asf/cordova-ios/diff/6bb1a85b
Branch: refs/heads/multipart_plugin_result
Commit: 6bb1a85bf57a786e59c3045af35d06703c467352
Parents: 9096527
Author: Michal Mocny <mm...@gmail.com>
Authored: Mon Feb 11 16:56:18 2013 -0500
Committer: Michal Mocny <mm...@gmail.com>
Committed: Mon Feb 25 08:38:09 2013 -0500
----------------------------------------------------------------------
CordovaLib/Classes/CDVCommandDelegateImpl.m | 12 +----
CordovaLib/Classes/CDVPluginResult.h | 4 ++
CordovaLib/Classes/CDVPluginResult.m | 49 +++++++++++++++++++--
3 files changed, 51 insertions(+), 14 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/6bb1a85b/CordovaLib/Classes/CDVCommandDelegateImpl.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVCommandDelegateImpl.m b/CordovaLib/Classes/CDVCommandDelegateImpl.m
index 7779880..5197cb5 100644
--- a/CordovaLib/Classes/CDVCommandDelegateImpl.m
+++ b/CordovaLib/Classes/CDVCommandDelegateImpl.m
@@ -89,15 +89,9 @@
}
int status = [result.status intValue];
BOOL keepCallback = [result.keepCallback boolValue];
- id message = result.message == nil ? [NSNull null] : result.message;
-
- // Use an array to encode the message as JSON.
- message = [NSArray arrayWithObject:message];
- NSString* encodedMessage = [message JSONString];
- // And then strip off the outer []s.
- encodedMessage = [encodedMessage substringWithRange:NSMakeRange(1, [encodedMessage length] - 2)];
- NSString* js = [NSString stringWithFormat:@"cordova.require('cordova/exec').nativeCallback('%@',%d,%@,%d)",
- callbackId, status, encodedMessage, keepCallback];
+ NSString* argumentsAsJSON = [result argumentsAsJSON];
+
+ NSString* js = [NSString stringWithFormat:@"cordova.require('cordova/exec').nativeCallback('%@',%d,%@,%d)", callbackId, status, argumentsAsJSON, keepCallback];
[self evalJsHelper:js];
}
http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/6bb1a85b/CordovaLib/Classes/CDVPluginResult.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVPluginResult.h b/CordovaLib/Classes/CDVPluginResult.h
index 8683205..8393df2 100644
--- a/CordovaLib/Classes/CDVPluginResult.h
+++ b/CordovaLib/Classes/CDVPluginResult.h
@@ -47,6 +47,7 @@ typedef enum {
+ (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal messageAsBool:(BOOL)theMessage;
+ (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal messageAsDictionary:(NSDictionary*)theMessage;
+ (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal messageAsArrayBuffer:(NSData*)theMessage;
++ (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal messageAsMultipart:(NSArray*)theMessages;
+ (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal messageToErrorObject:(int)errorCode;
+ (void)setVerbose:(BOOL)verbose;
@@ -54,6 +55,9 @@ typedef enum {
- (void)setKeepCallbackAsBool:(BOOL)bKeepCallback;
+- (NSString*)argumentsAsJSON;
+
+// These methods are used by the legacy plugin return result method
- (NSString*)toJSONString;
- (NSString*)toSuccessCallbackString:(NSString*)callbackId;
- (NSString*)toErrorCallbackString:(NSString*)callbackId;
http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/6bb1a85b/CordovaLib/Classes/CDVPluginResult.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVPluginResult.m b/CordovaLib/Classes/CDVPluginResult.m
index d9ba08f..a8f5689 100644
--- a/CordovaLib/Classes/CDVPluginResult.m
+++ b/CordovaLib/Classes/CDVPluginResult.m
@@ -33,6 +33,32 @@
static NSArray* org_apache_cordova_CommandStatusMsgs;
+id messageFromArrayBuffer(NSData* data)
+{
+ return [NSDictionary dictionaryWithObjectsAndKeys:
+ @"ArrayBuffer", @"CDVType",
+ [data base64EncodedString], @"data",
+ nil];
+}
+
+id messageFromMultipart(NSArray* theMessages)
+{
+ NSMutableArray* messages = [NSMutableArray arrayWithArray:theMessages];
+
+ for (NSUInteger i = 0; i < messages.count; ++i) {
+ id message = [messages objectAtIndex:i];
+
+ if ([message isKindOfClass:[NSData class]]) {
+ [messages replaceObjectAtIndex:i withObject:messageFromArrayBuffer(message)];
+ }
+ }
+
+ return [NSDictionary dictionaryWithObjectsAndKeys:
+ @"MultiPart", @"CDVType",
+ messages, @"messages",
+ nil];
+}
+
+ (void)initialize
{
org_apache_cordova_CommandStatusMsgs = [[NSArray alloc] initWithObjects:@"No result",
@@ -101,12 +127,12 @@ static NSArray* org_apache_cordova_CommandStatusMsgs;
+ (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal messageAsArrayBuffer:(NSData*)theMessage
{
- NSDictionary* arrDict = [NSDictionary dictionaryWithObjectsAndKeys:
- @"ArrayBuffer", @"CDVType",
- [theMessage base64EncodedString], @"data",
- nil];
+ return [[self alloc] initWithStatus:statusOrdinal message:messageFromArrayBuffer(theMessage)];
+}
- return [[self alloc] initWithStatus:statusOrdinal message:arrDict];
++ (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal messageAsMultipart:(NSArray*)theMessages
+{
+ return [[self alloc] initWithStatus:statusOrdinal message:messageFromMultipart(theMessages)];
}
+ (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal messageToErrorObject:(int)errorCode
@@ -121,6 +147,19 @@ static NSArray* org_apache_cordova_CommandStatusMsgs;
[self setKeepCallback:[NSNumber numberWithBool:bKeepCallback]];
}
+- (NSString*)argumentsAsJSON
+{
+ id arguments = (self.message == nil ? [NSNull null] : self.message);
+ NSArray* argumentsWrappedInArray = [NSArray arrayWithObject:arguments];
+
+ NSString* argumentsJSON = [argumentsWrappedInArray JSONString];
+
+ argumentsJSON = [argumentsJSON substringWithRange:NSMakeRange(1, [argumentsJSON length] - 2)];
+
+ return argumentsJSON;
+}
+
+// These methods are used by the legacy plugin return result method
- (NSString*)toJSONString
{
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys: