Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. [SOLVED]Registering default user preference on iOS
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]Registering default user preference on iOS

Scheduled Pinned Locked Moved Mobile and Embedded
ios
4 Posts 2 Posters 1.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DRoscoe
    wrote on last edited by DRoscoe
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

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

        D Offline
        D Offline
        DRoscoe
        wrote on last edited by
        #3

        @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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Glad you found out !

          Don't worry, the question was good nonetheless

          Thanks for sharing your solution

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved