Retrieving data from Settings.bundle (iOS Settings)

PhilippBartelsPhilippBartels Philipp BartelsUSMember

Hello I'm new to xamarin iOS developping.

I want to get the data stored in the App Settings.
I Created the settings how it was shown in this Thread http://forums.xamarin.com/discussion/1407/custom-settings-in-ios-settings-app/p1

I tried this code:
var ipadress = NSUserDefaults.StandardUserDefaults.StringForKey("Server_IP_Adress");

plist looks like this:

<dict>
            <key>AutocapitalizationType</key>
            <string>None</string>
            <key>AutocorrectionType</key>
            <string>No</string>
            <key>Server_IP_Adress</key>
            <string>1.2.3.4</string>
            <key>IsSecure</key>
            <false/>
            <key>Key</key>
            <string>name_preference</string>
            <key>KeyboardType</key>
            <string>NumbersAndPunctuation</string>
            <key>Title</key>
            <string>IP-Adresse</string>
            <key>Type</key>
            <string>PSTextFieldSpecifier</string>
        </dict>

I want the Code to retrieve 1.2.3.4 or an other Value if its changed in App settings.
Can you Help me?

Posts

  • BradRobinsonBradRobinson Brad Robinson AUMember ✭✭✭

    Hi Philipp

    You've got a couple of mistakes in your xml. The string you pass to StringForKey is the value associated with the key "Key".

    <key>Key</key>
    <string>Server_IP_Adress</string>
    

    The default value should be assigned to a key "DefaultValue"

    <key>DefaultValue</key>
    <string>1.2.3.4</string>
    

    Also, you didn't mention if the settings screen was showing up in the settings app - for that you need to make sure you've got the correct build action on the Root.plist (and that it's called Root.plist) and it's in a resource folder called "Settings.bundle".

    I've attached a test program that has all of this in-place, including how to get notified when the setting changes.

  • PhilippBartelsPhilippBartels Philipp Bartels USMember

    Thank you that work for me.

  • TravisSchillingTravisSchilling Travis Schilling USMember

    I realize it's been a while since this question was asked, but I downloaded the SettingBundleTest solution and the IP-Address entry doesn't even show up in the iOS settings. The SettingBundleTest settings pane is empty. I've tried this on iOS 6.1 and 7.0 simulators and an iOS 7 iPad 3.

    Any help would be greatly appreciated.

  • TravisSchillingTravisSchilling Travis Schilling USMember

    I'm not sure what exactly has changed since the solution was posted, but after another read of https://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Conceptual/UserDefaults/Preferences/Preferences.html#//apple_ref/doc/uid/10000059i-CH6-SW18, I decided to take the part, "A Settings bundle has the name Settings.bundle and resides in the top-level directory of your app’s bundle", literally and moved the Settings.bundle folder to be out of the Resources folder and at the project root. Then it worked.

  • YellowbeardYellowbeard Dirk Eismann USMember

    So what is the officially and preferred location for the Settings.Bundle folder then?

    When doing native iOS development it is supposed to live in the top-level directory.

    Using Xamarin we can place it anywhere while making sure that the Root.plist Build Action is set to BundleResource?

    Other posts here indicate that needs to live in the top-level and Root.plist Build Action should be set to Content...

    Is both fine? Any pros/cons?

  • ricardoaoricardoao RICARDO AGUILAR OSORIO MXMember

    Hi everyone, I'm developing in Xamarin for iOS, I have a doubt:

    I want to show the configuration settings inside my app, I check the Apple documentation in https://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Conceptual/UserDefaults/Preferences/Preferences.html

    But I cannot find how to achieve in Xamarin...

  • DaveHuntDaveHunt David Hunt USMember ✭✭✭✭✭
    edited March 2014

    The documentation you linked does not discuss showing configuration settings inside your app. It describes creating a settings bundle so that your app's settings show up inside the built-in iOS Settings app. It does not involve any code (it's just plist files and localized string resources) so would have no equivalent in Xamarin.

    To show configuration settings inside your app, you simply provide a page or set of pages with your app's configuration options and provide a button or menu item somewhere to display the page(s). Your app would then write/read the users choices to/from a file.

Sign In or Register to comment.