react-native-ios-settings-bundle icon indicating copy to clipboard operation
react-native-ios-settings-bundle copied to clipboard

value is empty or the key is not defined

Open RenanAlvesBCC opened this issue 6 years ago • 5 comments

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",

RenanAlvesBCC avatar Dec 06 '19 18:12 RenanAlvesBCC

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

vijaychhetry avatar Apr 01 '20 10:04 vijaychhetry

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

danielgpinheiro avatar Apr 01 '20 16:04 danielgpinheiro

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

moreiraj2 avatar May 19 '20 13:05 moreiraj2

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

qdanik avatar Nov 06 '20 09:11 qdanik

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?

sdevansh96 avatar Feb 15 '21 15:02 sdevansh96