iDevGames Forums
Problems with CFPreferences*() - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html)
+--- Thread: Problems with CFPreferences*() (/thread-8217.html)



Problems with CFPreferences*() - sealfin - Oct 16, 2010 03:54 AM

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! Smile

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;
}



RE: Problems with CFPreferences*() - OneSadCookie - Oct 16, 2010 11:46 AM

Completely unhelpful response: since you're using kCFPreferencesCurrentApplication and ObjC, why not NSUserDefaults?


RE: Problems with CFPreferences*() - Frogblast - Oct 16, 2010 02:07 PM

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.


RE: Problems with CFPreferences*() - sealfin - Oct 16, 2010 03:40 PM

@OneSadCookie: because I'm more comfortable with C than with Objective-C Wink

@Frogblast: yeah, that was the solution Blush 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 Blink

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;
}



RE: Problems with CFPreferences*() - OneSadCookie - Oct 16, 2010 04:28 PM

Or it could look like:

Code:
void WriteSInt16ToPreferences(char const *label, SInt16 n)
{
    [[NSUserDefaults standardUserDefaults] setInteger:n forKey:[NSString stringWithUTF8String:label]];
}