Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS - setItem function always returns the wrong callback (Error storing item) #156

Open
sirineKr opened this issue Nov 2, 2020 · 0 comments

Comments

@sirineKr
Copy link

sirineKr commented Nov 2, 2020

Hello,
I use this plugin with the Ionic4 framework. It works fine on Android but on iOS, the setItem function always returns "Error storing item" even if the data is well recorded.

if (this.deviceInfo.isiOSDevice()) {
  this.nativeStorage.initWithSuiteName(environment.appGroup);
  this.nativeStorage.setItem('encryptionMetaData', { 'key': this.metadata.key, 'algo': 
this.metadata.alg, 'transformation': this.metadata.transformation })
    .then(
       () => console.log('Stored item!'),
        error => console.error('Error storing item', error)
     );

  this.nativeStorage.getItem('encryptionMetaData')
  .then(
    data => console.log(data),
    error => console.error(error)
  );
}

In the native code (in setItem function) in debug mode, success variable always returns "no".

NSUserDefaults *defaults = [self getUserDefault];
[defaults setObject: aString forKey:reference];
BOOL success = [defaults synchronize];
if(success) pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString:aString];
else pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString:@"Write has failed"];

When, I change the condition in setItem function with the following code, I have the right callback

NSUserDefaults *defaults = [self getUserDefault];
[defaults setObject: aString forKey:reference];
[defaults synchronize];
if([[defaults stringForKey:reference] isEqualToString:aString]){
   pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString:aString];
 }
else pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString:@"Write has failed"];

With the current plugin code, I don't know why the synchronize method always returns false.
From what I have seen on the Internet, this method is unnecessary and shouldn't be used since iOS 12.
Could this be the source of the problem?

For information, even in the case of the clear() function, the synchronize method always returns false.

Thanks in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant