[SOLVED]Registering default user preference on iOS



  • I have an iOS Settings.bundle which specifies the following in the root.plist file

    			<string>PSTextFieldSpecifier</string>
    			<key>Title</key>
    			<string>TAP Engine Hostname: </string>
    			<key>DefaultValue</key>
    			<string>localhost</string>
    			<key>Key</key>
    			<string>engineHostname</string>
    

    The problem is, when I query the value of engineHostname, it comes back as a null string. I did a little research and discovered that this is how it works. The default values have to be registered to set them in the user preferences database before they are available to be queried

    My question is, how can this be done through Qt? I suppose I could query my user preferences and see if they are valid. If not, I could then manually set them to default values, but this seems clunky to me.


  • Lifetime Qt Champion

    Hi,

    I've responded there but we might as well continue here since you made a new topic about it



  • @SGaist I have solved the problem. First, I was trying to solve the wrong problem. Parsing the .plist file was not what I really wanted. It only sets up what gets stored in the user preference database which is maintained outside of the .plist file. The problem then became, how to read these values from the database. I was trying to find a "Qt way" to do this, but I couldn't find one, which is why I posted this.

    As it turned out, the solution was trivial once I looked at it outside of Qt. The "Qt way" in this instance was using the ability to include Objective C files in my project. Because the Clang compiler can do mixed C++/Objective C compilation, I leveraged this to implement the following:

    //
    //  UserPreferenceUtility.h
    //  tap_new_display_app
    //
    //  Created by David Roscoe on 5/21/15.
    //
    //
    
    #ifndef tap_new_display_app_UserPreferenceUtility_h
    #define tap_new_display_app_UserPreferenceUtility_h
    
    class QString;
    
    class UserPreferenceUtility
    {
    public:
      
      /// register default preferences from plist file
      void registerDefaultPrefs(void);
      
      /// get currently stored engine hostname
      QString getEngineHostname(void);
      
    private:  // methods
      
    };
    
    #endif
    

    This sets up the C++ interface for the Objective C code implemented here:

    //
    //  UserPreferenceUtility.m
    //  tap_new_display_app
    //
    //  Created by David Roscoe on 5/19/15.
    //
    //
    
    #include "UserPreferenceUtility.h"
    #import <Foundation/Foundation.h>
    #include <QtCore>
    
    // ----------------------------------------------------------------------------
    
    void UserPreferenceUtility::registerDefaultPrefs()
    {
      NSDictionary* appDefaults = [NSDictionary
                                   dictionaryWithObject:@"localhost" forKey:@"engineHostname"];
      
      if (appDefaults)
      {
        // the default value was found in the dictionary.  Register it.
        // wrapping this in an if.. clause prevents overwriting a user entered
        // value with the default value
        [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
      }
    }
    
    // ----------------------------------------------------------------------------
    
    QString UserPreferenceUtility::getEngineHostname()
    {
      NSString* engine_hostname_pref = [[NSUserDefaults standardUserDefaults] stringForKey:@"engineHostname"];
      return QString::fromNSString(engine_hostname_pref);
    }
    

    The ability to mix C++ and Objective C in the .mm file, allowed me to make native calls to get the information I needed.

    Thanks again for your help. Sorry I was asking the wrong questions, but I honestly didn't understand how it all worked.

    -Dave


  • Lifetime Qt Champion

    Glad you found out !

    Don't worry, the question was good nonetheless

    Thanks for sharing your solution


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.