Last Updated: February 25, 2016
·
368
· raulsilvamx

NSDictionary to iCloud iOS and NSDictionary to JSON

Well the other day i've treated save NSDictionary to iCloud but only execute the command.

//code for save NSDictionary to iCloud value: NSDictionary key:NSString
[[NSUbiquitousKeyValueStore defaultStore] setDictionary:value forKey:key];

it doesn't save. So after many times i found the solution...

Well if you use the next code with your NSDictionary. You can test before submit to iCloud. if return error you won't save a NSDictonary to iCloud unless you fix it.

+ (NSString*) dictionaryToJSON:(NSDictionary*)dictionary{
NSString *JSON;

NSError *error;
NSData *JSONData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error]; 
if (error) {
    NSLog(@"dictionaryToJSON error: %@", error);
    return JSON;
}    
JSON =[[NSString alloc] initWithData:JSONData encoding:NSUTF8StringEncoding];
return JSON;
}

Thank You... See you later...!