Advertisement
Guest User

Untitled

a guest
May 9th, 2017
73
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.         [AWSCognito registerCognitoWithConfiguration:configuration forKey:@"CognitoSync"];
  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.                                 CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"SignIn successful"];
  149.                                 [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  150.                             }
  151.                         });
  152.                         return nil;
  153.                     }];
  154.                 }
  155.             });
  156.             return nil;
  157.         }];
  158.     }
  159.  
  160.     - (void)signOut:(CDVInvokedUrlCommand *)command {
  161.         self.User = [self.Pool currentUser];
  162.  
  163.         if (![self.CognitoIdentityUserPoolAppClientSecret isKindOfClass:[NSNull class]]) {
  164.             NSLog(@"!!!!!!!!!!! getIdentityId will start inside signOut");
  165.             [[self.credentialsProvider getIdentityId] continueWithBlock:^id _Nullable(AWSTask<NSString *> * _Nonnull task) {
  166.                 dispatch_async(dispatch_get_main_queue(), ^{
  167.                     if(task.error){
  168.                         NSLog(@"!!!!!!!!!!! getIdentityId inside SignOut, error : %@", task.error.userInfo);
  169.                         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:task.error.userInfo[@"NSLocalizedDescription"]];
  170.                         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  171.                     } else {
  172.                         NSLog(@"!!!!!!!!!!! getIdentityId inside SignOut, task.result : %@", task.result);
  173.  
  174.                         [self.User signOut];
  175.  
  176.                         [self.credentialsProvider clearKeychain];
  177.  
  178.                         // Wipe all data (test aws)
  179.                         // Every thing work, if we wipe data but the offline data are wipe obviously
  180.                         // [self.syncClient wipe];
  181.  
  182.                         MyManager *sharedManager = [MyManager sharedManager];
  183.  
  184.                         sharedManager.lastUsername = @"";
  185.                         sharedManager.lastPassword = @"";
  186.  
  187.                         self.dataset = nil;
  188.  
  189.                         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"SignOut successful"];
  190.                         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  191.                     }
  192.                 });
  193.                 return nil;
  194.             }];
  195.         }
  196.         else {
  197.             CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"No user connected"];
  198.             [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];          
  199.         }
  200.     }
  201.  
  202.     - (void)signUp:(CDVInvokedUrlCommand*)command{
  203.         NSMutableDictionary* options = [command.arguments objectAtIndex:0];
  204.  
  205.         NSString *passwordString = [options objectForKey:@"password"];
  206.         NSString *idString = [options objectForKey:@"id"];
  207.  
  208.         NSMutableArray* attributes = [options objectForKey:@"attributes"];
  209.         NSMutableArray* attributesToSend = [NSMutableArray new];
  210.  
  211.         NSUInteger size = [attributes count];
  212.  
  213.         for (int i = 0; i < size; i++)
  214.         {
  215.             NSMutableDictionary* attributesIndex = [attributes objectAtIndex:i];
  216.  
  217.             AWSCognitoIdentityUserAttributeType * tmp = [AWSCognitoIdentityUserAttributeType new];
  218.  
  219.             tmp.name  = [attributesIndex objectForKey:@"name"];
  220.             tmp.value = [attributesIndex objectForKey:@"value"];
  221.  
  222.             [attributesToSend addObject:tmp];
  223.         }
  224.  
  225.         //sign up the user
  226.         [[self.Pool signUp:idString password:passwordString userAttributes:attributesToSend validationData:nil] continueWithBlock:^id _Nullable(AWSTask<AWSCognitoIdentityUserPoolSignUpResponse *> * _Nonnull task) {
  227.             dispatch_async(dispatch_get_main_queue(), ^{
  228.                 if(task.error){
  229.                     NSLog(@"error : %@", task.error);
  230.                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:task.error.userInfo];
  231.                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  232.                 } else{
  233.                     AWSCognitoIdentityUserPoolSignUpResponse * response = task.result;
  234.                     if(!response.userConfirmed){
  235.                         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:true];
  236.                         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  237.                     }
  238.                     else {
  239.                         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:false];
  240.                         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];                      
  241.                     }
  242.                 }});
  243.             return nil;
  244.         }];
  245.     }
  246.  
  247.     - (void)confirmSignUp:(CDVInvokedUrlCommand*)command{
  248.         NSMutableDictionary* options = [command.arguments objectAtIndex:0];
  249.  
  250.         NSString *tokenString = [options objectForKey:@"token"];
  251.         NSString *idString = [options objectForKey:@"id"];
  252.  
  253.         if (idString) {
  254.             self.User = [self.Pool getUser:idString];
  255.         }
  256.  
  257.         [[self.User confirmSignUp:tokenString forceAliasCreation:YES] continueWithBlock: ^id _Nullable(AWSTask<AWSCognitoIdentityUserConfirmSignUpResponse *> * _Nonnull task) {
  258.             dispatch_async(dispatch_get_main_queue(), ^{
  259.                 if(task.error){
  260.                     NSLog(@"error : %@", task.error);
  261.                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:task.error.userInfo];
  262.                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  263.                 } else {
  264.                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"good token"];
  265.                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  266.                 }
  267.             });
  268.             return nil;
  269.         }];
  270.     }
  271.  
  272.     - (void)forgotPassword:(CDVInvokedUrlCommand*)command{
  273.         NSMutableDictionary* options = [command.arguments objectAtIndex:0];
  274.  
  275.         NSString *idString = [options objectForKey:@"id"];        
  276.  
  277.         if (idString) {
  278.             self.User = [self.Pool getUser:idString];
  279.         }
  280.  
  281.         [[self.User forgotPassword] continueWithBlock:^id _Nullable(AWSTask<AWSCognitoIdentityUserForgotPasswordResponse *> * _Nonnull task) {
  282.             dispatch_async(dispatch_get_main_queue(), ^{
  283.                 if(task.error){
  284.                     NSLog(@"error : %@", task.error);
  285.                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:task.error.userInfo];
  286.                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  287.                 } else {
  288.                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"good token"];
  289.                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  290.                 }
  291.             });
  292.             return nil;
  293.         }];
  294.     }
  295.  
  296.     - (void)updatePassword:(CDVInvokedUrlCommand*)command {
  297.         NSMutableDictionary* options = [command.arguments objectAtIndex:0];
  298.  
  299.         NSString *confirmationCode = [options objectForKey:@"confirmationCode"];
  300.         NSString *newPassword = [options objectForKey:@"newPassword"];
  301.  
  302.         [[self.User confirmForgotPassword:confirmationCode password:newPassword] continueWithBlock:^id _Nullable(AWSTask<AWSCognitoIdentityUserConfirmForgotPasswordResponse *> * _Nonnull task) {
  303.             dispatch_async(dispatch_get_main_queue(), ^{
  304.                 if(task.error){
  305.                     NSLog(@"error : %@", task.error);
  306.                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:task.error.userInfo];
  307.                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  308.                 } else {
  309.                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"good token"];
  310.                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  311.                 }
  312.             });
  313.             return nil;
  314.         }];
  315.     }
  316.  
  317.     -(void)getDetails:(CDVInvokedUrlCommand*)command {
  318.         AWSCognitoIdentityProviderGetUserRequest* request = [AWSCognitoIdentityProviderGetUserRequest new];
  319.         request.accessToken = self.actualAccessToken.tokenString;
  320.  
  321.         AWSCognitoIdentityProvider *defaultIdentityProvider = [AWSCognitoIdentityProvider defaultCognitoIdentityProvider];
  322.  
  323.         [[defaultIdentityProvider getUser:request] continueWithBlock:^id _Nullable(AWSTask<AWSCognitoIdentityProviderGetUserResponse *> * _Nonnull task) {
  324.             dispatch_async(dispatch_get_main_queue(), ^{
  325.                 if(task.error){
  326.                     NSLog(@"error : %@", task.error);
  327.                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:task.error.userInfo];
  328.                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  329.                 } else {
  330.                     AWSCognitoIdentityProviderGetUserResponse *response = task.result;
  331.  
  332.                     NSMutableDictionary *toReturn= [NSMutableDictionary dictionary];
  333.                     NSUInteger size = [response.userAttributes count];
  334.  
  335.                     for (int i = 0; i < size; i++)
  336.                     {
  337.                         toReturn[response.userAttributes[i].name] = response.userAttributes[i].value;
  338.                     }
  339.  
  340.                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:toReturn];
  341.                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  342.                 }
  343.             });
  344.             return nil;
  345.         }];
  346.     }
  347.  
  348.     - (void)resendConfirmationCode:(CDVInvokedUrlCommand*)command {
  349.         NSMutableDictionary* options = [command.arguments objectAtIndex:0];
  350.  
  351.         NSString *idString = [options objectForKey:@"id"];        
  352.  
  353.         if (idString) {
  354.             self.User = [self.Pool getUser:idString];
  355.         }
  356.  
  357.         [[self.User resendConfirmationCode] continueWithBlock:^id _Nullable(AWSTask<AWSCognitoIdentityUserResendConfirmationCodeResponse *> * _Nonnull task) {
  358.             dispatch_async(dispatch_get_main_queue(), ^{
  359.                 if(task.error){
  360.                     NSLog(@"error : %@", task.error);
  361.                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:task.error.userInfo[@"message"]];
  362.                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  363.                 } else {
  364.                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"OK"];
  365.                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  366.                 }
  367.             });
  368.             return nil;
  369.         }];
  370.     }
  371.  
  372.     /*
  373.     ** Cognito Sync
  374.     */
  375.  
  376.     - (void) createAWSCognitoDataset:(CDVInvokedUrlCommand *) command {
  377.         // Add a dictionnary to allow to open multiple database
  378.  
  379.         NSMutableDictionary* options = [command.arguments objectAtIndex:0];
  380.  
  381.         NSString *idString = [options objectForKey:@"id"];
  382.         NSString *cognitoId = self.credentialsProvider.identityId;
  383.  
  384.         NSLog(@"createAWSCognitoDataset idString : %@", idString);
  385.         NSLog(@"createAWSCognitoDataset cognitoId : %@", cognitoId);
  386.  
  387.         AWSCognito *syncClient = [AWSCognito CognitoForKey:@"CognitoSync"];
  388.  
  389.         if ([[Reachability reachabilityForInternetConnection]currentReachabilityStatus] == NotReachable) {
  390.             self.dataset = [syncClient openOrCreateDataset:idString];
  391.  
  392.             self.dataset.conflictHandler = ^AWSCognitoResolvedConflict* (NSString *datasetName, AWSCognitoConflict *conflict) {
  393.                 // override and always choose remote changes
  394.                 return [conflict resolveWithRemoteRecord];
  395.             };
  396.             CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"NetworkingError"];
  397.             [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  398.         }
  399.         else {
  400.             [[syncClient refreshDatasetMetadata] continueWithBlock:^id(AWSTask *task) {
  401.                 dispatch_async(dispatch_get_main_queue(), ^{
  402.                     if (task.error){
  403.                         NSLog(@"createAWSCognitoDataset refreshDatasetMetadata error : %@", task.error);
  404.                         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:task.error];
  405.                         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  406.                     }
  407.                     else {
  408.                         NSLog(@"createAWSCognitoDataset refreshDatasetMetadata success : %@", task.result);
  409.                         self.dataset = [syncClient openOrCreateDataset:idString];
  410.  
  411.                         self.dataset.conflictHandler = ^AWSCognitoResolvedConflict* (NSString *datasetName, AWSCognitoConflict *conflict) {
  412.                             // override and always choose remote changes
  413.                             return [conflict resolveWithRemoteRecord];
  414.                         };
  415.                        
  416.                         if ([[Reachability reachabilityForInternetConnection]currentReachabilityStatus] == NotReachable) {
  417.                             CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"NetworkingError"];
  418.                             [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  419.                         }
  420.                         else {
  421.                             [[self.dataset synchronize] continueWithBlock:^id(AWSTask *task) {
  422.                                 dispatch_async(dispatch_get_main_queue(), ^{
  423.                                     if (task.isCancelled) {
  424.                                         NSLog(@"createAWSCognitoDataset isCancelled : %@", task.isCancelled);
  425.                                         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Canceled"];
  426.                                         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  427.                                     }
  428.                                     else if(task.error){
  429.                                         NSLog(@"createAWSCognitoDataset error : %@", task.error);
  430.                                         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:task.error.userInfo];
  431.                                         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  432.                                     } else {
  433.                                         NSLog(@"createAWSCognitoDataset success : %@", task.result);
  434.                                         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"createAWSCognitoDataset Successful"];
  435.                                         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  436.                                     }
  437.                                 });
  438.                                 return nil;
  439.                             }];
  440.                         }
  441.                     }
  442.                 });
  443.                 return nil;
  444.             }];
  445.         }
  446.     }
  447.  
  448.  
  449.     - (void) getUserDataCognitoSync:(CDVInvokedUrlCommand *) command {
  450.         NSMutableDictionary* options = [command.arguments objectAtIndex:0];
  451.         NSString *keyString = [options objectForKey:@"key"];
  452.  
  453.         NSString *value = [self.dataset stringForKey:keyString];
  454.  
  455.         NSLog(@"getUserDataCognitoSync, value : %@", value);
  456.         NSLog(@"getUserDataCognitoSync, keyString: %@", keyString);
  457.  
  458.         if ([[Reachability reachabilityForInternetConnection]currentReachabilityStatus] == NotReachable) {
  459.             NSLog(@"getUserDataCognitoSync failed NetworkingError");
  460.             CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:value];
  461.             [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  462.         }
  463.         else {
  464.             [[self.dataset synchronize] continueWithBlock:^id(AWSTask *task) {
  465.                 dispatch_async(dispatch_get_main_queue(), ^{
  466.                     if (task.isCancelled) {
  467.                         NSLog(@"getUserDataCognitoSync isCancelled : %@", task.isCancelled);
  468.                         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Canceled"];
  469.                         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  470.                     }
  471.                     else if(task.error){
  472.                         NSLog(@"getUserDataCognitoSync error : %@", task.error);
  473.                         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:task.error.userInfo];
  474.                         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  475.                     } else {
  476.                         NSLog(@"getUserDataCognitoSync success : %@", value);
  477.                         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:value];
  478.                         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  479.                     }
  480.                 });
  481.                 return nil;
  482.             }];
  483.         }
  484.     }
  485.  
  486.     - (void) setUserDataCognitoSync:(CDVInvokedUrlCommand *) command {
  487.         NSString *identityId = self.credentialsProvider.identityId;
  488.         NSMutableDictionary* options = [command.arguments objectAtIndex:0];
  489.  
  490.         NSString *keyString = [options objectForKey:@"key"];
  491.         NSString *valueString = [options objectForKey:@"value"];
  492.  
  493.         [self.dataset setString:valueString forKey:keyString];
  494.         if ([[Reachability reachabilityForInternetConnection]currentReachabilityStatus] == NotReachable) {
  495.             CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"NetworkingError, data saved localy"];
  496.             [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  497.         }
  498.         else {
  499.             [[self.dataset synchronize] continueWithBlock:^id(AWSTask *task) {
  500.                 dispatch_async(dispatch_get_main_queue(), ^{
  501.                     if (task.isCancelled) {
  502.                         NSLog(@"isCancelled : %@", task.isCancelled);
  503.                         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Canceled"];
  504.                         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  505.                     }
  506.                     else if(task.error){
  507.                         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Error"];
  508.                         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  509.                     } else {
  510.                         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"setUserDataCognitoSync Successful"];
  511.                         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  512.                     }
  513.                 });
  514.                 return nil;
  515.             }];
  516.         }
  517.     }
  518.  
  519.     - (void)callAWSLambdaFunction:(CDVInvokedUrlCommand*) command {
  520.         /*
  521.         // Not generic yet, only work for me.
  522.         // Need to find a way to call function from the aws linked file
  523.         */
  524.  
  525.         NSMutableDictionary* options = [command.arguments objectAtIndex:0];
  526.  
  527.         NSString *username = [options objectForKey:@"username"];
  528.  
  529.         BYMAPPV3User *user = [[BYMAPPV3User alloc] init];
  530.         user.userName = username;
  531.  
  532.         BYMAPPV3BYMAPPClient *apiInstance = [BYMAPPV3BYMAPPClient clientForKey:@"EUWest1BYMAPPV3BYMAPPClient"];
  533.  
  534.         [[apiInstance userapiUserPost:user] continueWithBlock:^id(AWSTask *task) {
  535.             dispatch_async(dispatch_get_main_queue(), ^{
  536.                 if(task.error) {
  537.                     NSLog(@"error : %@", task.error);
  538.                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:task.error.userInfo];
  539.                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  540.                 } else {
  541.                     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"Ok"];
  542.                     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  543.                 }
  544.             });
  545.             return nil;
  546.         }];
  547.     }
  548.  
  549.     @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement