Problems with CFPreferences*()

Moderator
Posts: 702
Joined: 2002.04
Post: #1
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;
}

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
Completely unhelpful response: since you're using kCFPreferencesCurrentApplication and ObjC, why not NSUserDefaults?
Quote this message in a reply
Member
Posts: 87
Joined: 2006.08
Post: #3
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.
Quote this message in a reply
Moderator
Posts: 702
Joined: 2002.04
Post: #4
@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;
}

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
Or it could look like:

Code:
void WriteSInt16ToPreferences(char const *label, SInt16 n)
{
    [[NSUserDefaults standardUserDefaults] setInteger:n forKey:[NSString stringWithUTF8String:label]];
}
Quote this message in a reply
Post Reply