Detect changes on NSUserDefaults

后端 未结 5 2074
你的背包
你的背包 2020-12-29 23:45

I\'m developing an iOS application with latest SDK.

I want to know when a property on NSUserDefaults changes it value.

I have found this, but it

5条回答
  •  隐瞒了意图╮
    2020-12-30 00:44

    Use NSUserDefaultsDidChangeNotification for notification about change in User defaults:

    [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(defaultsDidChange:) name:NSUserDefaultsDidChangeNotification
        object:nil];
    
    // notification
    - (void)defaultsDidChange:(NSNotification *)aNotification
    {
         //
    }
    

    Use KVO for notification about specific change in User defaults:

    [[NSUserDefaults standardUserDefaults] addObserver:self 
        forKeyPath:@"APXMyPropertyIamInterestedInKey" options:NSKeyValueObservingOptionNew
        context:NULL];
    
    // KVO handler
    -(void)observeValueForKeyPath:(NSString *)aKeyPath ofObject:(id)anObject
        change:(NSDictionary *)aChange context:(void *)aContext 
    {
        // 
    }
    

提交回复
热议问题