On iOS "monitor" NSUserDefaults read and writes -


in ios project, using 3rd party library (google vr), reads , writes stuff nsuserdefaults.

i know can read , print user default (by like

nsarray *keys = [[[nsuserdefaults standarduserdefaults] dictionaryrepresentation] allkeys];  for(nsstring* key in keys){     // code here     nslog(@"value: %@ forkey: %@",[[nsuserdefaults standarduserdefaults] valueforkey:key],key); } 

what need see library looking , not not finding.
basically, library checks if has done configuration before (pairing cardboard headset), checking userdefaults key. want know when happens , ket is.

i understand 1 way diff, print user defaults before , after pairing , see changed, want know if there general way monitor nsuserdefaults read calls.

method swizzling, sounds possible solution, not quite sure how work.

edit: record, used answer @blackm below , found out google vr unity, on ios looks com.google.cardboard.sdk.deviceparamsandtime check if has configured headset or not.

i manage implement method swizzling:

#import <objc/runtime.h>  @implementation nsuserdefaults (read) +(void)load {     static dispatch_once_t oncetoken;     dispatch_once(&oncetoken, ^{         class class = [self class];          //swizzling objectforkey         sel originalselectorvwa = @selector(objectforkey:);         sel swizzledselectorvwa = @selector(swizzled_objectforkey:);          method originalmethodvwa = class_getinstancemethod(class, originalselectorvwa);         method swizzledmethodvwa = class_getinstancemethod(class, swizzledselectorvwa);          bool didaddmethodvwa =         class_addmethod(class,                         originalselectorvwa,                         method_getimplementation(swizzledmethodvwa),                         method_gettypeencoding(swizzledmethodvwa));          if (didaddmethodvwa) {             class_replacemethod(class,                                 swizzledselectorvwa,                                 method_getimplementation(originalmethodvwa),                                 method_gettypeencoding(originalmethodvwa));         } else {             method_exchangeimplementations(originalmethodvwa, swizzledmethodvwa);         }      }); }  #pragma mark - method swizzling - (id)swizzled_objectforkey:(nsstring *)defaultname {      id data = [self swizzled_objectforkey:defaultname];      if (!data) {          nsdictionary *infodict = [nsdictionary dictionarywithobject:defaultname forkey:@"key"];         [[nsnotificationcenter defaultcenter] postnotificationname:@"readnsuserdefaults" object:nil userinfo:infodict];     }      return data; } @end 

in controller:

- (void)viewdidload {      [super viewdidload];      nsstring *valuetosave = @"somevalue";      [[nsuserdefaults standarduserdefaults] setobject:valuetosave forkey:@"somekey"];      [[nsuserdefaults standarduserdefaults] synchronize];      [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(readnsuserdefaults:) name:@"readnsuserdefaults" object:nil];        [[nsuserdefaults standarduserdefaults] stringforkey:@"somekey"]; [[nsuserdefaults standarduserdefaults] stringforkey:@"thiskeydoesntexistkey"]; }  -(void)readnsuserdefaults:(nsnotification*)notification {      nsstring *key = [notification.userinfo objectforkey:@"key"];     nslog(@"read key %@ returned nil nsuserdefaults",key); } 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -