In the vast majority of cases, this will not be a meaningful way to serialize your object into NSData:
MyObject *myObject = [[MyObject alloc] init]; NSData *myObjectData = [NSData dataWithBytes:(void *)&myObject length:sizeof(myObject)]; [[NSUserDefaults standardUserDefaults] setObject:myObjectData forKey:@"kMyObjectData"];
The canonical way to do this would be for MyObject to adopt the NSCoding protocol. Based on the code you posted here, accepting NSCoding might look like this:
- (id)initWithCoder:(NSCoder *)coder { if (self = [super init]) { mConsumer = [coder decodeObjectForKey: @"consumer"]; mToken = [coder decodeObjectForKey: @"token"]; } return self; } - (void)encodeWithCoder:(NSCoder *)coder { [coder encodeObject:mConsumer forKey: @"consumer"]; [coder encodeObject:mToken forKey:@"token"]; }
Once you have completed this work, you should convert MyObject to and from NSData as follows:
NSData* data = [NSKeyedArchiver archivedDataWithRootObject: myObject]; MyObject* myObject = (MyObject*)[NSKeyedUnarchiver unarchiveObjectWithData: data];
The code that you have here will completely destroy the stack and crash (because this line [getData getBytes:&getObject]; will cause NSData to write bytes to the getObject address that is locally declared on the stack. Therefore, stack splitting). Starting with your code, a working implementation might look something like this:
- (IBAction)linkedInLog:(UIButton *)sender { NSData* dataFromDefaults = [[NSUserDefaults standardUserDefaults] objectForKey:@"linkedinfo"]; LinkedContainer* getObject = (LinkedContainer*)[NSKeyedUnarchiver unarchiveObjectWithData: dataFromDefaults]; if (!dataFromDefaults) { mLogInView = [[linkedInLoginView alloc]initWithNibName:@"linkedInLogInView" bundle:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginViewDidFinish:) name:@"loginViewDidFinish" object:mLogInView]; [self.navigationController pushViewController:mLogInView animated:YES]; if ((FBSession.activeSession.isOpen)&&(mLinkedInIsLogegOn)) { mMergeButton.hidden = NO; } } else{ mLinkedInIsLogegOn= YES; mLinkedInInfo.mConsumer = getObject.mConsumer; mLinkedInInfo.mToken = getObject.mToken; } } -(void) loginViewDidFinish:(NSNotification*)notification { [[NSNotificationCenter defaultCenter] removeObserver:self]; mLinkedInInfo.mConsumer = mLogInView.consumer; mLinkedInInfo.mToken = mLogInView.accessToken; NSData* objectData = [NSKeyedArchiver archivedDataWithRootObject: mLinkedInInfo]; [[NSUserDefaults standardUserDefaults] setObject: objectData forKey: @"linkedinfo"]; [[NSUserDefaults standardUserDefaults] synchronize]; if (mLinkedInInfo.mToken) { mLinkedInIsLogegOn = YES; } }
ipmcc source share