Advertisement
Guest User

Untitled

a guest
May 3rd, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import "AwsUserPoolPlugin.h"
  2.  
  3.     @implementation MyManager
  4.  
  5.     @synthesize lastUsername;
  6.     @synthesize lastPassword;
  7.  
  8.     + (id)sharedManager {
  9.         static MyManager *sharedMyManager = nil;
  10.         static dispatch_once_t onceToken;
  11.  
  12.         dispatch_once(&onceToken, ^{
  13.             sharedMyManager = [[self alloc] init];
  14.         });
  15.         return sharedMyManager;
  16.     }
  17.  
  18.     - (id)init {
  19.       if (self = [super init]) {
  20.             lastUsername = [[NSString alloc] initWithString:@""];
  21.             lastPassword = [[NSString alloc] initWithString:@""];
  22.       }
  23.       return self;
  24.     }
  25.  
  26.     @end
  27.  
  28.     @implementation AWSCognitoIdentityUserPool (UserPoolsAdditions)
  29.  
  30.     - (AWSTask<NSString *> *)token {        
  31.         MyManager *sharedManager = [MyManager sharedManager];
  32.  
  33.         return [[[self currentUser] getSession:sharedManager.lastUsername password:sharedManager.lastPassword validationData:nil]
  34.                 continueWithSuccessBlock:^id _Nullable(AWSTask<AWSCognitoIdentityUserSession *> * _Nonnull task) {
  35.                     return [AWSTask taskWithResult:task.result.idToken.tokenString];
  36.                 }];
  37.     }
  38.  
  39.     @end
  40.  
  41.     @implementation AwsUserPoolPlugin
  42.  
  43.     AWSRegionType const CognitoIdentityUserPoolRegion = AWSRegionEUWest1;
  44.  
  45.     - (void)init:(CDVInvokedUrlCommand*)command{
  46.         [AWSDDLog sharedInstance].logLevel = AWSDDLogLevelVerbose;
  47.         [AWSDDLog addLogger:[AWSDDTTYLogger sharedInstance]];
  48.  
  49.         NSMutableDictionary* options = [command.arguments objectAtIndex:0];
  50.  
  51.         self.CognitoIdentityUserPoolId = [options objectForKey:@"CognitoIdentityUserPoolId"];
  52.         self.CognitoIdentityUserPoolAppClientId = [options objectForKey:@"CognitoIdentityUserPoolAppClientId"];
  53.         self.CognitoIdentityUserPoolAppClientSecret = [options objectForKey:@"CognitoIdentityUserPoolAppClientSecret"];
  54.         if(!self.CognitoIdentityUserPoolAppClientSecret || [self.CognitoIdentityUserPoolAppClientSecret isKindOfClass:[NSNull class]]
  55.             || self.CognitoIdentityUserPoolAppClientSecret.length)
  56.             self.CognitoIdentityUserPoolAppClientSecret = nil;
  57.         self.User = nil;
  58.         self.actualAccessToken = nil;
  59.         self.arnIdentityPoolId = [options objectForKey:@"arnIdentityPoolId"];
  60.         self.dataset = nil;
  61.  
  62.         self.credentialsProvider = nil;
  63.  
  64.         AWSServiceConfiguration *serviceConfiguration = [[AWSServiceConfiguration alloc] initWithRegion:CognitoIdentityUserPoolRegion credentialsProvider:nil];
  65.  
  66.         AWSCognitoIdentityUserPoolConfiguration *userPoolConfiguration = [[AWSCognitoIdentityUserPoolConfiguration alloc] initWithClientId:self.CognitoIdentityUserPoolAppClientId clientSecret:self.CognitoIdentityUserPoolAppClientSecret poolId:self.CognitoIdentityUserPoolId];
  67.  
  68.         [AWSCognitoIdentityUserPool registerCognitoIdentityUserPoolWithConfiguration:serviceConfiguration userPoolConfiguration:userPoolConfiguration forKey:@"UserPool"];
  69.  
  70.         self.Pool = [AWSCognitoIdentityUserPool CognitoIdentityUserPoolForKey:@"UserPool"];
  71.  
  72.         self.credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:CognitoIdentityUserPoolRegion identityPoolId:self.arnIdentityPoolId identityProviderManager:self.Pool];
  73.  
  74.         AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:CognitoIdentityUserPoolRegion credentialsProvider:self.credentialsProvider];
  75.         [AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;
  76.        
  77.         // Not generic BYMAPPV3BYMAPPClient is something from my application
  78.         [BYMAPPV3BYMAPPClient registerClientWithConfiguration:configuration forKey:@"EUWest1BYMAPPV3BYMAPPClient"];
  79.  
  80.         self.syncClient = [AWSCognito defaultCognito];
  81.  
  82.         MyManager *sharedManager = [MyManager sharedManager];
  83.  
  84.         sharedManager.lastUsername = @"";
  85.         sharedManager.lastPassword = @"";
  86.  
  87.         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"Initialization successful"];
  88.  
  89.         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  90.     }
  91.  
  92.     - (void)offlineSignIn:(CDVInvokedUrlCommand*)command {
  93.         /*
  94.         // The SignIn will always return true, you need to manage the signin on the cordova side.
  95.         // This function is needed if you already signin your user with internet and you want him to access to his data even in offline mode
  96.         */
  97.         NSMutableDictionary* options = [command.arguments objectAtIndex:0];
  98.  
  99.         NSString *username = [options objectForKey:@"username"];
  100.         NSString *password = [options objectForKey:@"password"];
  101.  
  102.         MyManager *sharedManager = [MyManager sharedManager];
  103.  
  104.         sharedManager.lastUsername = username;
  105.         sharedManager.lastPassword = password;
  106.  
  107.         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"SignIn offline successful"];
  108.         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  109.     }
  110.  
  111.     - (void)signIn:(CDVInvokedUrlCommand*)command{
  112.         NSMutableDictionary* options = [command.arguments objectAtIndex:0];
  113.  
  114.         NSString *username = [options objectForKey:@"username"];
  115.         NSString *password = [options objectForKey:@"password"];
  116.  
  117.         self.User = [self.Pool getUser:username];
  118.    
  119.         [[self.User getSession:username password:password validationData:nil] continueWithBlock:^id _Nullable(AWSTask<AWSCognitoIdentityUserSession *> * _Nonnull task) {
  120.             dispatch_async(dispatch_get_main_queue(), ^{
  121.                 if(task.error){
  122.                     NSLog(@"error : %@", task.error.userInfo);
  123.                     MyManager *sharedManager = [MyManager sharedManager];
  124.  
  125.                     sharedManager.lastUsername = username;
  126.                     sharedManager.lastPassword = password;
  127.  
  128.                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:task.error.userInfo];
  129.                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  130.                 } else{
  131.                     self.actualAccessToken = task.result.accessToken;
  132.  
  133.                     MyManager *sharedManager = [MyManager sharedManager];
  134.  
  135.                     sharedManager.lastUsername = username;
  136.                     sharedManager.lastPassword = password;
  137.  
  138.                     NSLog(@"!!!!!!!!!!! getIdentityId will start inside signIn");
  139.                     [[self.credentialsProvider getIdentityId] continueWithBlock:^id _Nullable(AWSTask<NSString *> * _Nonnull task) {
  140.                         dispatch_async(dispatch_get_main_queue(), ^{
  141.                             if(task.error){
  142.                                 NSLog(@"!!!!!!!!!!! getIdentityId inside signIn, error : %@", task.error.userInfo);
  143.                                 CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:task.error.userInfo[@"NSLocalizedDescription"]];
  144.                                 [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  145.                             } else {
  146.                                 NSLog(@"!!!!!!!!!!! getIdentityId inside signIn, task.result : %@", task.result);
  147.  
  148.                                 [[NSNotificationCenter defaultCenter]
  149.                                     postNotificationName:AWSCognitoIdentityIdChangedNotification
  150.                                     object:self.Pool];
  151.  
  152.                                 CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"SignIn successful"];
  153.                                 [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  154.                             }
  155.                         });
  156.                         return nil;
  157.                     }];
  158.                 }
  159.             });
  160.             return nil;
  161.         }];
  162.     }
  163.  
  164.     - (void)signOut:(CDVInvokedUrlCommand *)command {
  165.         self.User = [self.Pool currentUser];
  166.  
  167.         if (![self.CognitoIdentityUserPoolAppClientSecret isKindOfClass:[NSNull class]]) {
  168.             NSLog(@"!!!!!!!!!!! getIdentityId will start inside signOut");
  169.             [[self.credentialsProvider getIdentityId] continueWithBlock:^id _Nullable(AWSTask<NSString *> * _Nonnull task) {
  170.                 dispatch_async(dispatch_get_main_queue(), ^{
  171.                     if(task.error){
  172.                         NSLog(@"!!!!!!!!!!! getIdentityId inside SignOut, error : %@", task.error.userInfo);
  173.                         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:task.error.userInfo[@"NSLocalizedDescription"]];
  174.                         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  175.                     } else {
  176.                         NSLog(@"!!!!!!!!!!! getIdentityId inside SignOut, task.result : %@", task.result);
  177.  
  178.                         [self.User signOut];
  179.  
  180.                         [self.credentialsProvider clearKeychain];
  181.  
  182.                         [self.Pool clearLastKnownUser];
  183.  
  184.                         // Wipe all data (test aws)
  185.                         // Every thing work, if we wipe data but the offline data are wipe obviously
  186.                         // [self.syncClient wipe];
  187.  
  188.                         MyManager *sharedManager = [MyManager sharedManager];
  189.  
  190.                         sharedManager.lastUsername = @"";
  191.                         sharedManager.lastPassword = @"";
  192.  
  193.                         self.dataset = nil;
  194.  
  195.                         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"SignOut successful"];
  196.                         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  197.                     }
  198.                 });
  199.                 return nil;
  200.             }];
  201.         }
  202.         else {
  203.             CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"No user connected"];
  204.             [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];          
  205.         }
  206.     }
  207.  
  208.     - (void)signUp:(CDVInvokedUrlCommand*)command{
  209.         NSMutableDictionary* options = [command.arguments objectAtIndex:0];
  210.  
  211.         NSString *passwordString = [options objectForKey:@"password"];
  212.         NSString *idString = [options objectForKey:@"id"];
  213.  
  214.         NSMutableArray* attributes = [options objectForKey:@"attributes"];
  215.         NSMutableArray* attributesToSend = [NSMutableArray new];
  216.  
  217.         NSUInteger size = [attributes count];
  218.  
  219.         for (int i = 0; i < size; i++)
  220.         {
  221.             NSMutableDictionary* attributesIndex = [attributes objectAtIndex:i];
  222.  
  223.             AWSCognitoIdentityUserAttributeType * tmp = [AWSCognitoIdentityUserAttributeType new];
  224.  
  225.             tmp.name  = [attributesIndex objectForKey:@"name"];
  226.             tmp.value = [attributesIndex objectForKey:@"value"];
  227.  
  228.             [attributesToSend addObject:tmp];
  229.         }
  230.  
  231.         //sign up the user
  232.         [[self.Pool signUp:idString password:passwordString userAttributes:attributesToSend validationData:nil] continueWithBlock:^id _Nullable(AWSTask<AWSCognitoIdentityUserPoolSignUpResponse *> * _Nonnull task) {
  233.             dispatch_async(dispatch_get_main_queue(), ^{
  234.                 if(task.error){
  235.                     NSLog(@"error : %@", task.error);
  236.                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:task.error.userInfo];
  237.                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  238.                 } else{
  239.                     AWSCognitoIdentityUserPoolSignUpResponse * response = task.result;
  240.                     if(!response.userConfirmed){
  241.                         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:true];
  242.                         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  243.                     }
  244.                     else {
  245.                         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:false];
  246.                         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];                      
  247.                     }
  248.                 }});
  249.             return nil;
  250.         }];
  251.     }
  252.  
  253.     - (void)confirmSignUp:(CDVInvokedUrlCommand*)command{
  254.         NSMutableDictionary* options = [command.arguments objectAtIndex:0];
  255.  
  256.         NSString *tokenString = [options objectForKey:@"token"];
  257.         NSString *idString = [options objectForKey:@"id"];
  258.  
  259.         if (idString) {
  260.             self.User = [self.Pool getUser:idString];
  261.         }
  262.  
  263.         [[self.User confirmSignUp:tokenString forceAliasCreation:YES] continueWithBlock: ^id _Nullable(AWSTask<AWSCognitoIdentityUserConfirmSignUpResponse *> * _Nonnull task) {
  264.             dispatch_async(dispatch_get_main_queue(), ^{
  265.                 if(task.error){
  266.                     NSLog(@"error : %@", task.error);
  267.                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:task.error.userInfo];
  268.                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  269.                 } else {
  270.                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"good token"];
  271.                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  272.                 }
  273.             });
  274.             return nil;
  275.         }];
  276.     }
  277.  
  278.     - (void)forgotPassword:(CDVInvokedUrlCommand*)command{
  279.         NSMutableDictionary* options = [command.arguments objectAtIndex:0];
  280.  
  281.         NSString *idString = [options objectForKey:@"id"];        
  282.  
  283.         if (idString) {
  284.             self.User = [self.Pool getUser:idString];
  285.         }
  286.  
  287.         [[self.User forgotPassword] continueWithBlock:^id _Nullable(AWSTask<AWSCognitoIdentityUserForgotPasswordResponse *> * _Nonnull task) {
  288.             dispatch_async(dispatch_get_main_queue(), ^{
  289.                 if(task.error){
  290.                     NSLog(@"error : %@", task.error);
  291.                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:task.error.userInfo];
  292.                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  293.                 } else {
  294.                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"good token"];
  295.                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  296.                 }
  297.             });
  298.             return nil;
  299.         }];
  300.     }
  301.  
  302.     - (void)updatePassword:(CDVInvokedUrlCommand*)command {
  303.         NSMutableDictionary* options = [command.arguments objectAtIndex:0];
  304.  
  305.         NSString *confirmationCode = [options objectForKey:@"confirmationCode"];
  306.         NSString *newPassword = [options objectForKey:@"newPassword"];
  307.  
  308.         [[self.User confirmForgotPassword:confirmationCode password:newPassword] continueWithBlock:^id _Nullable(AWSTask<AWSCognitoIdentityUserConfirmForgotPasswordResponse *> * _Nonnull task) {
  309.             dispatch_async(dispatch_get_main_queue(), ^{
  310.                 if(task.error){
  311.                     NSLog(@"error : %@", task.error);
  312.                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:task.error.userInfo];
  313.                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  314.                 } else {
  315.                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"good token"];
  316.                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  317.                 }
  318.             });
  319.             return nil;
  320.         }];
  321.     }
  322.  
  323.     -(void)getDetails:(CDVInvokedUrlCommand*)command {
  324.         AWSCognitoIdentityProviderGetUserRequest* request = [AWSCognitoIdentityProviderGetUserRequest new];
  325.         request.accessToken = self.actualAccessToken.tokenString;
  326.  
  327.         AWSCognitoIdentityProvider *defaultIdentityProvider = [AWSCognitoIdentityProvider defaultCognitoIdentityProvider];
  328.  
  329.         [[defaultIdentityProvider getUser:request] continueWithBlock:^id _Nullable(AWSTask<AWSCognitoIdentityProviderGetUserResponse *> * _Nonnull task) {
  330.             dispatch_async(dispatch_get_main_queue(), ^{
  331.                 if(task.error){
  332.                     NSLog(@"error : %@", task.error);
  333.                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:task.error.userInfo];
  334.                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  335.                 } else {
  336.                     AWSCognitoIdentityProviderGetUserResponse *response = task.result;
  337.  
  338.                     NSMutableDictionary *toReturn= [NSMutableDictionary dictionary];
  339.                     NSUInteger size = [response.userAttributes count];
  340.  
  341.                     for (int i = 0; i < size; i++)
  342.                     {
  343.                         toReturn[response.userAttributes[i].name] = response.userAttributes[i].value;
  344.                     }
  345.  
  346.                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:toReturn];
  347.                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  348.                 }
  349.             });
  350.             return nil;
  351.         }];
  352.     }
  353.  
  354.     - (void)resendConfirmationCode:(CDVInvokedUrlCommand*)command {
  355.         NSMutableDictionary* options = [command.arguments objectAtIndex:0];
  356.  
  357.         NSString *idString = [options objectForKey:@"id"];        
  358.  
  359.         if (idString) {
  360.             self.User = [self.Pool getUser:idString];
  361.         }
  362.  
  363.         [[self.User resendConfirmationCode] continueWithBlock:^id _Nullable(AWSTask<AWSCognitoIdentityUserResendConfirmationCodeResponse *> * _Nonnull task) {
  364.             dispatch_async(dispatch_get_main_queue(), ^{
  365.                 if(task.error){
  366.                     NSLog(@"error : %@", task.error);
  367.                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:task.error.userInfo[@"message"]];
  368.                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  369.                 } else {
  370.                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"OK"];
  371.                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  372.                 }
  373.             });
  374.             return nil;
  375.         }];
  376.     }
  377.  
  378.     /*
  379.     ** Cognito Sync
  380.     */
  381.  
  382.     - (void) createAWSCognitoDataset:(CDVInvokedUrlCommand *) command {
  383.         // Add a dictionnary to allow to open multiple database
  384.  
  385.         NSMutableDictionary* options = [command.arguments objectAtIndex:0];
  386.  
  387.         NSString *idString = [options objectForKey:@"id"];
  388.         NSString *cognitoId = self.credentialsProvider.identityId;
  389.  
  390.         NSLog(@"createAWSCognitoDataset idString : %@", idString);
  391.         NSLog(@"createAWSCognitoDataset cognitoId : %@", cognitoId);
  392.  
  393.        
  394.         [[self.syncClient refreshDatasetMetadata] continueWithBlock:^id(AWSTask *task) {
  395.             dispatch_async(dispatch_get_main_queue(), ^{
  396.                 if (task.error){
  397.                     NSLog(@"createAWSCognitoDataset refreshDatasetMetadata error : %@", task.error);
  398.                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:task.error];
  399.                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  400.                 }
  401.                 else {
  402.                     NSLog(@"createAWSCognitoDataset refreshDatasetMetadata success : %@", task.result);
  403.                     self.dataset = [self.syncClient openOrCreateDataset:idString];
  404.  
  405.                     self.dataset.conflictHandler = ^AWSCognitoResolvedConflict* (NSString *datasetName, AWSCognitoConflict *conflict) {
  406.                         // override and always choose remote changes
  407.                         return [conflict resolveWithRemoteRecord];
  408.                     };
  409.                    
  410.                     if ([[Reachability reachabilityForInternetConnection]currentReachabilityStatus] == NotReachable) {
  411.                         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"NetworkingError"];
  412.                         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  413.                     }
  414.                     else {
  415.                         [[self.dataset synchronize] continueWithBlock:^id(AWSTask *task) {
  416.                             dispatch_async(dispatch_get_main_queue(), ^{
  417.                                 if (task.isCancelled) {
  418.                                     NSLog(@"createAWSCognitoDataset isCancelled : %@", task.isCancelled);
  419.                                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Canceled"];
  420.                                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  421.                                 }
  422.                                 else if(task.error){
  423.                                     NSLog(@"createAWSCognitoDataset error : %@", task.error);
  424.                                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:task.error.userInfo];
  425.                                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  426.                                 } else {
  427.                                     NSLog(@"createAWSCognitoDataset success : %@", task.result);
  428.                                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"createAWSCognitoDataset Successful"];
  429.                                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  430.                                 }
  431.                             });
  432.                             return nil;
  433.                         }];
  434.                     }
  435.                 }
  436.             });
  437.             return nil;
  438.         }];
  439.     }
  440.  
  441.  
  442.     - (void) getUserDataCognitoSync:(CDVInvokedUrlCommand *) command {
  443.         NSMutableDictionary* options = [command.arguments objectAtIndex:0];
  444.         NSString *keyString = [options objectForKey:@"key"];
  445.  
  446.         NSString *value = [self.dataset stringForKey:keyString];
  447.  
  448.         NSLog(@"getUserDataCognitoSync, value : %@", value);
  449.         NSLog(@"getUserDataCognitoSync, keyString: %@", keyString);
  450.  
  451.         if ([[Reachability reachabilityForInternetConnection]currentReachabilityStatus] == NotReachable) {
  452.             NSLog(@"getUserDataCognitoSync failed NetworkingError");
  453.             CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:value];
  454.             [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  455.         }
  456.         else {
  457.             [[self.dataset synchronize] continueWithBlock:^id(AWSTask *task) {
  458.                 dispatch_async(dispatch_get_main_queue(), ^{
  459.                     if (task.isCancelled) {
  460.                         NSLog(@"getUserDataCognitoSync isCancelled : %@", task.isCancelled);
  461.                         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Canceled"];
  462.                         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  463.                     }
  464.                     else if(task.error){
  465.                         NSLog(@"getUserDataCognitoSync error : %@", task.error);
  466.                         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:task.error.userInfo];
  467.                         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  468.                     } else {
  469.                         NSLog(@"getUserDataCognitoSync success : %@", value);
  470.                         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:value];
  471.                         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  472.                     }
  473.                 });
  474.                 return nil;
  475.             }];
  476.         }
  477.     }
  478.  
  479.     - (void) setUserDataCognitoSync:(CDVInvokedUrlCommand *) command {
  480.         NSString *identityId = self.credentialsProvider.identityId;
  481.         NSMutableDictionary* options = [command.arguments objectAtIndex:0];
  482.  
  483.         NSString *keyString = [options objectForKey:@"key"];
  484.         NSString *valueString = [options objectForKey:@"value"];
  485.  
  486.         [self.dataset setString:valueString forKey:keyString];
  487.         if ([[Reachability reachabilityForInternetConnection]currentReachabilityStatus] == NotReachable) {
  488.             CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"NetworkingError, data saved localy"];
  489.             [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  490.         }
  491.         else {
  492.             [[self.dataset synchronize] continueWithBlock:^id(AWSTask *task) {
  493.                 dispatch_async(dispatch_get_main_queue(), ^{
  494.                     if (task.isCancelled) {
  495.                         NSLog(@"isCancelled : %@", task.isCancelled);
  496.                         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Canceled"];
  497.                         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  498.                     }
  499.                     else if(task.error){
  500.                         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Error"];
  501.                         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  502.                     } else {
  503.                         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"setUserDataCognitoSync Successful"];
  504.                         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  505.                     }
  506.                 });
  507.                 return nil;
  508.             }];
  509.         }
  510.     }
  511.  
  512.     - (void)callAWSLambdaFunction:(CDVInvokedUrlCommand*) command {
  513.         /*
  514.         // Not generic yet, only work for me.
  515.         // Need to find a way to call function from the aws linked file
  516.         */
  517.  
  518.         NSMutableDictionary* options = [command.arguments objectAtIndex:0];
  519.  
  520.         NSString *username = [options objectForKey:@"username"];
  521.  
  522.         BYMAPPV3User *user = [[BYMAPPV3User alloc] init];
  523.         user.userName = username;
  524.  
  525.         BYMAPPV3BYMAPPClient *apiInstance = [BYMAPPV3BYMAPPClient clientForKey:@"EUWest1BYMAPPV3BYMAPPClient"];
  526.  
  527.         [[apiInstance userapiUserPost:user] continueWithBlock:^id(AWSTask *task) {
  528.             dispatch_async(dispatch_get_main_queue(), ^{
  529.                 if(task.error) {
  530.                     NSLog(@"error : %@", task.error);
  531.                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:task.error.userInfo];
  532.                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  533.                 } else {
  534.                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"Ok"];
  535.                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  536.                 }
  537.             });
  538.             return nil;
  539.         }];
  540.     }
  541.  
  542.     @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement