I am developing an app in iOS 16 ARC and I am trying to persist a string value inside NSUserDefaults like this:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
// Retrieve the selected image
UIImage *selectedImage = info[UIImagePickerControllerOriginalImage];
// Dismiss the image picker controller
[picker dismissViewControllerAnimated:YES completion:^{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *base64String = [self getImageBase64:selectedImage];
[defaults setObject:base64String forKey:@"imageBase64"];
// [defaults synchronize]; //has no effect
NSLog(@"retrieved: %@", [defaults objectForKey:@"imageBase64"]);
}];
}
However, the NSLog
statement always prints a blank string. I originally thought this was related to memory management and the base64String
falling outside of scope once the method completed execution, but it doesn’t seem to be working even when I try to reference it within the same scope/function execution.
Any help would be greatly appreciated!