Search code examples
iosbackgroundnsuserdefaultssynchronizedsettings-bundle

Once I changed the setting in Settings Bundle, Can my App get the change at the same time?


I know I can synchronize NSUserDefaults when my App enter foreground. But it can't meet my requirements, I want once I change setting in the Setting Bundle, my App's volume can change at the same time, even if it's run in background.


Solution

  • I think, You can't change volume when app run in background(even manually)!!.

    However You can detect the volume settings changes when apps wake from background using NSUserDefaultsDidChangeNotification

    Example:

    -(void)viewDidAppear:(BOOL)animated{
    
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(defaultsSettingsChanged) name:NSUserDefaultsDidChangeNotification object:nil];    
      }
    
    - (void)viewDidDisappear:(BOOL)animated{
    
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    }
    
    -(void)defaultsSettingsChanged{
    
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
        if([[defaults stringForKey:@"speech"] isEqualToString:@"0"])
        {
            speech.hidden = YES;
        }
    }