Commit a359fa58 authored by Jason Farnsworth's avatar Jason Farnsworth
Browse files

Getting the plugin to register on iOS in a Cordova 3.0 environment

The signature of all the methods callable from JavaScript had to change
from:
-- (void)register:(NSMutableArray *)arguments
withDict:(NSMutableDictionary *)options;
to
+- (void)register:(CDVInvokedUrlCommand*)command;

The old format was officially obsoleted in in Cordova 3.0, so there is
no way around using the old one.

'arguments' and 'options' needed to use the new 'command' object.

With this new format the plugin result interface was also broken, so it
was changed to the new documented format.

This code is _not_ well tested at this point, but I wanted to get it up
for anyone having issues migrating to 3.0.
parent fe01c166
Loading
Loading
Loading
Loading
+2 −1
Changes for src/ios/PushPlugin.h: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@

#import <Foundation/Foundation.h>
#import <Cordova/CDV.h>
#import <Cordova/CDVPlugin.h>

@interface PushPlugin : CDVPlugin
{
@@ -43,7 +44,7 @@
@property (nonatomic, strong) NSDictionary *notificationMessage;
@property BOOL                          isInline;

- (void)register:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options;
- (void)register:(CDVInvokedUrlCommand*)command;

- (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;
- (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;
+10 −6
Changes for src/ios/PushPlugin.m: 10 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -35,17 +35,19 @@
@synthesize callback;


- (void)unregister:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options
- (void)unregister:(CDVInvokedUrlCommand*)command;
{
	self.callbackId = [arguments pop];
	self.callbackId = command.callbackId;

    [[UIApplication sharedApplication] unregisterForRemoteNotifications];
    [self successWithMessage:@"unregistered"];
}

- (void)register:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options
- (void)register:(CDVInvokedUrlCommand*)command;
{
	self.callbackId = [arguments pop];
	self.callbackId = command.callbackId;

    NSMutableDictionary* options = [command.arguments objectAtIndex:0];

    UIRemoteNotificationType notificationTypes = UIRemoteNotificationTypeNone;
    id badgeArg = [options objectForKey:@"badge"];
@@ -89,11 +91,13 @@
		[self notificationReceived];	// go ahead and process it
}

/*
- (void)isEnabled:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options {
    UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
    NSString *jsStatement = [NSString stringWithFormat:@"navigator.PushPlugin.isEnabled = %d;", type != UIRemoteNotificationTypeNone];
    NSLog(@"JSStatement %@",jsStatement);
}
*/

- (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

@@ -226,7 +230,7 @@
{
    CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:message];
    
    [self writeJavascript:[commandResult toSuccessCallbackString:self.callbackId]];
    [self.commandDelegate sendPluginResult:commandResult callbackId:self.callbackId];
}

-(void)failWithMessage:(NSString *)message withError:(NSError *)error
@@ -234,7 +238,7 @@
    NSString        *errorMessage = (error) ? [NSString stringWithFormat:@"%@ - %@", message, [error localizedDescription]] : message;
    CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:errorMessage];
    
    [self writeJavascript:[commandResult toErrorCallbackString:self.callbackId]];
    [self.commandDelegate sendPluginResult:commandResult callbackId:self.callbackId];
}

@end