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

value is empty or the key is not defined #1

Open
RenanAlvesBCC opened this issue Dec 6, 2019 · 5 comments
Open

value is empty or the key is not defined #1

RenanAlvesBCC opened this issue Dec 6, 2019 · 5 comments

Comments

@RenanAlvesBCC
Copy link

RenanAlvesBCC commented Dec 6, 2019

after i create my Setting.Bundle:
Captura de Tela 2019-12-06 às 3 23 04 PM
and set this code lines:
RNIosSettingsBundle.get('url_reference',(err,value)=>{ console.log('sucesso ===',value); console.log('errooooo ======',err); })
I got this message:
Captura de Tela 2019-12-06 às 3 20 43 PM

Info project:

  • "react": "16.9.0",
  • "react-native": "0.61.5",
  • "react-native-ios-settings-bundle": "^1.2.2",
@vijaychhetry
Copy link

Is this issue is resolved?
I am getting same issue for multi value default value is not getting.

@danielgpinheiro
Copy link

Hello guys, I was using this lib today and I also came across this error. In the attempts here, I realized that if you insert a Default Value into an item, that lib cannot get the value. I created a group with 3 inputs, without the Default Value, and managed to get the values.

image

@moreiraj2
Copy link

moreiraj2 commented May 19, 2020

To solve this issue with this implementation .
Adding a function to registerDefaultsFromSettingsBundle and call this function on applicationDidFinishLaunching

My implementation:
AppDelegate.m

[...]
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  
  #if DEBUG
    NSLog(@"Registering default values from Settings.bundle");
    NSUserDefaults * defs = [NSUserDefaults standardUserDefaults];
    [defs synchronize];
     
    NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];
     
    if(!settingsBundle)
    {
       NSLog(@"Could not find Settings.bundle");
    }else{
      NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[settingsBundle stringByAppendingPathComponent:@"Root.plist"]];
      NSArray *preferences = [settings objectForKey:@"PreferenceSpecifiers"];
      NSMutableDictionary *defaultsToRegister = [[NSMutableDictionary alloc] initWithCapacity:[preferences count]];
       
      for (NSDictionary *prefSpecification in preferences)
      {
         NSString *key = [prefSpecification objectForKey:@"Key"];
         if (key)
         {
            // check if value readable in userDefaults
            id currentObject = [defs objectForKey:key];
            if (currentObject == nil)
            {
               // not readable: set value from Settings.bundle
               id objectToSet = [prefSpecification objectForKey:@"DefaultValue"];
               [defaultsToRegister setObject:objectToSet forKey:key];
               NSLog(@"Setting object %@ for key %@", objectToSet, key);
               }
            else
               {
               // already readable: don't touch
               NSLog(@"Key %@ is readable (value: %@), nothing written to defaults.", key, currentObject);
               }
            }
         }
       
      [defs registerDefaults:defaultsToRegister];
      [defs synchronize];
    }
  #endif

[...]

Output log:

Registering default values from Settings.bundle
Setting object 0 for key custom_endpoint_enabled
Setting object QA for key environment_url

@qdanik
Copy link

qdanik commented Nov 6, 2020

Maybe anyone wants to have a set method or asynchronous get method. See attached code below

Edit File: RNIosSettingsBundle.m

RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(getValByKey:(NSString *)key)
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    return [defaults objectForKey:key];
}

RCT_EXPORT_METHOD(setValByKey:(NSString *)key value:(NSString *)value)
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults setObject:value forKey:key];
}

In the future, I will try to create a separate repo with the ability to listen to changes. If anyone needs it

@sdevansh96
Copy link

Hello ,

Any update on the issue?

Can anyone help

Maybe anyone wants to have a set method or asynchronous get method. See attached code below

Edit File: RNIosSettingsBundle.m

RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(getValByKey:(NSString *)key)
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    return [defaults objectForKey:key];
}

RCT_EXPORT_METHOD(setValByKey:(NSString *)key value:(NSString *)value)
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults setObject:value forKey:key];
}

In the future, I will try to create a separate repo with the ability to listen to changes. If anyone needs it

Hello @qdanik,

Have you created the repo?

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

6 participants