Problems with CFPreferences*()
Greetings, I'm currently developing an app for a client and have hit a problem: that preferences are broken, not being written (or read?) by CFPreferences* on the client's devices; complicating the problem is that preferences are not broken on any of my devices (iOS 2.x through to 4.x); I'm just wondering whether anybody can take a look at the code below and suggest where the problem might lie? Thanks! 

Code:
void WriteSInt16ToPreferences( const char *p_label, const SInt16 p_no )
{
NSString *label = [[ NSString alloc ] initWithCString:p_label encoding:[ NSString defaultCStringEncoding ]];
CFNumberRef no = CFNumberCreate( kCFAllocatorDefault, kCFNumberSInt16Type, &p_no );
CFPreferencesSetAppValue(( CFStringRef )label, no, kCFPreferencesCurrentApplication );
[ label release ];
CFRelease( no );
return;
}Code:
bool ReadSInt16FromPreferences( const char *p_label, SInt16 *p_no )
{
NSString *label = [[ NSString alloc ] initWithCString:p_label encoding:[ NSString defaultCStringEncoding ]];
CFNumberRef no = ( CFNumberRef )CFPreferencesCopyAppValue(( CFStringRef )label, kCFPreferencesCurrentApplication );
[ label release ];
if( no != NULL )
{
CFNumberGetValue( no, kCFNumberSInt16Type, p_no );
return true;
}
return false;
}Mark Bishop
Completely unhelpful response: since you're using kCFPreferencesCurrentApplication and ObjC, why not NSUserDefaults?
Do you need to use this API or something like it to ensure that the changes were saved to the filesystem?
/* Writes all changes in all sources of application defaults. Returns success or failure. */
CF_EXPORT Boolean CFPreferencesAppSynchronize(CFStringRef applicationID);
I have only ever used NSUserDefaults, which I am fairly sure synchronizes for you as you iterate through the event loop.
/* Writes all changes in all sources of application defaults. Returns success or failure. */
CF_EXPORT Boolean CFPreferencesAppSynchronize(CFStringRef applicationID);
I have only ever used NSUserDefaults, which I am fairly sure synchronizes for you as you iterate through the event loop.
@OneSadCookie: because I'm more comfortable with C than with Objective-C 
@Frogblast: yeah, that was the solution
I went through the documentation (again) a few minutes after I posted the thread, but hadn't been back to update the thread... I'm not sure how I missed that when I went through the documentation previously; I'm even more unsure of why that code functioned in all my other apps (for two years) in iPhone Simulator.app and on all of my devices 
The updated function looks like this:

@Frogblast: yeah, that was the solution
I went through the documentation (again) a few minutes after I posted the thread, but hadn't been back to update the thread... I'm not sure how I missed that when I went through the documentation previously; I'm even more unsure of why that code functioned in all my other apps (for two years) in iPhone Simulator.app and on all of my devices 
The updated function looks like this:
Code:
void WriteSInt16ToPreferences( const char *p_label, const SInt16 p_no )
{
NSString *label = [[ NSString alloc ] initWithCString:p_label encoding:[ NSString defaultCStringEncoding ]];
CFNumberRef no = CFNumberCreate( kCFAllocatorDefault, kCFNumberSInt16Type, &p_no );
CFPreferencesSetAppValue(( CFStringRef )label, no, kCFPreferencesCurrentApplication );
[ label release ];
CFRelease( no );
CFPreferencesAppSynchronize( kCFPreferencesCurrentApplication );
return;
}Mark Bishop
Or it could look like:
Code:
void WriteSInt16ToPreferences(char const *label, SInt16 n)
{
[[NSUserDefaults standardUserDefaults] setInteger:n forKey:[NSString stringWithUTF8String:label]];
}
