iOS app settings

Source: Internet
Author: User
<span id="Label3"></p><p><p>today, even the simplest computer program contains a preferences window where users can set options that are unique to the app. In Mac OS x, Preferences ... Menus are usually located in the app Menu. Selecting the menu item pops up a window where the user can enter and change various options. The iphone and other iOS devices have a dedicated "settings" app to make various settings, and you've definitely used it many Times.</p></p>Set up Bundles<p><p>By applying settings, users can enter and change preferences in any app with a provisioning bundle (settings bundle). A provisioning bundle is a set of files that the app comes with to tell you what preferences the app expects from the User. is an example:</p></p><p><p></p></p><p><p>For the iOS user default setting (user Defaults) mechanism, Set the role that the app acts as a common user interface. The user default settings are part of the system that saves and gets preferences.</p></p><p><p>In iOS apps, the user default settings are implemented by the Nsuserdefaults class. The app reads and saves the preference data by Nsuserdefaults with Key-value pairs, just as you would get data from a Nsdictionary object through a key. The difference is that nsuserdefaults data is persisted in the file system, not in an object instance that is stored in memory.</p></p>Application<p><p>The next section is to implement the app settings control application by creating a simple Application.</p></p>Create an App<p><p>In xcode, create the tabbed application Project.</p></p>Using the provisioning bundle<p><p>The Settings app builds an App's settings view using the contents of the settings bundle in each app. If the app does not have a bundle set up, the Settings app does not show any information about the Application. Each provisioning bundle must contain a list of attributes named root.plist, which defines the Root-level preferences View. This list of properties must follow a very strict format.</p></p><p><p>When the Settings app starts, it examines the settings bundle for each application and adds a settings group for each app that contains the provisioning Bundle.</p></p>To add a provisioning bundle to a project<p><p>Create a new provisioning bundle that operates as</p></p><p><p></p></p><p><p>Click Next on the Ios-->resource-->settings bundle, the name remains the default, and the last click Create.</p></p>Edit Property List File<p><p>Next you need to edit the Root.plist file, as detailed</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>In Item8, refer to the More.plist file, specific content such as</p></p><p><p></p></p><p><p>Here is a point to add:</p></p><p><p>Apps on iOS can't read files from a sandbox in another app. Setting up bundles is not part of our app sandbox, but rather a part of the app Sandbox.</p></p>Design Presentation Interface<p><p>After the data is ready, add the icon resource and design the data presentation Interface. As follows</p></p><p><p></p></p><p><p>The interface is implemented through code, mainly using the following controls:</p></p><pre><pre> <span style="color: #000000;"> @interface firstviewcontroller () @property (nonatomic, strong) UILabel </span> *< Span style= "color: #000000;" >officerlabel; @property (nonatomic, Strong) UILabel *<span style="color: #000000;">authorizationcodelabel;@ Property (nonatomic, Strong) UILabel </span> *<span style="color: #000000;">ranklabel; @property (nonatomic, Strong) UILabel </span> *<span style="color: #000000;">warpdrivelabel; @property (nonatomic, strong) UILabel </span> *<span style="color: #000000;">warpfactorlabel; @property (nonatomic, Strong) UILabel </span> *<span style="color: #000000;">favoritetealabel;@ Property (nonatomic, Strong) UILabel </span> *<span style="color: #000000;">favoritecaptainlabel; @property ( nonatomic, Strong) UILabel </span> *<span style="color: #000000;">favoritegadgetlabel; @property (nonatomic, Strong) UILabel </span> *<span style="color: #000000;">favoritealienlabel; @end </span> </pre></pre><pre><pre><span style="color: #000000;"></span>* * * *<span style="color: #000000;">settinginfo; @end</span> <span style="color: #000000;"></span><span style="color: #000000;"></span></pre></pre>Read settings in an app<p><p>We will use the Nsuserdefaults class to access the user settings. Nsuserdefaults as a singleton class means that only one Nsuserdefaults instance is running in the Application. To access this instance, you need to call Standarduserdefaults.</p></p>Key macro Definition<pre><pre><span style="color: #000000;">#ifndef Bridgecontrol_constants_h</span> <span style="color: #0000ff;">#define</span> Bridgecontrol_constants_h<span style="color: #0000ff;">#define</span> kofficerkey @ "officer"<span style="color: #0000ff;">#define</span> kauthorizationcodekey @ " Authorizationcode "<span style="color: #0000ff;">#define</span> krankkey @" rank "<span style="color: #0000ff;">#define</span> kwarpdrivekey @" warp "<span style="color: #0000ff;">#define</span> Kwarpfactorkey @ "warpfactor"<span style="color: #0000ff;">#define</span> kfavoriteteakey @ "favoritetea"<span style="color: #0000ff;">#define</span> kfavoritecaptionkey @ " Favoritecaptain "<span style="color: #0000ff;">#define</span> kfavoritegadgetkey @" favoritegadget "<span style="color: #0000ff;">#define</span> Kfavoritealienkey @" Favoritealien "<span style="color: #0000ff;">#endif</span></pre></pre>Updating the master view Controller<pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">FIRSTVIEWCONTROLLER.M</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Viewdidload {[super viewdidload]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">additional setup after loading the view, typically from a nib.</span></span><span style="color: #000000;"><span style="color: #000000;">[self refreshfields];}</span></span>-(<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Refreshfields{nsuserdefaults</span></span>*defaults =<span style="color: #000000;"><span style="color: #000000;">[nsuserdefaults standarduserdefaults]; Self.officerLabel.text</span></span>=<span style="color: #000000;"><span style="color: #000000;">[defaults objectforkey:kofficerkey]; Self.authorizationCodeLabel.text</span></span>=<span style="color: #000000;"><span style="color: #000000;">[defaults objectforkey:kauthorizationcodekey]; Self.rankLabel.text</span></span>=<span style="color: #000000;"><span style="color: #000000;">[defaults objectforkey:krankkey]; Self.warpDriveLabel.text</span></span>= [defaults boolforkey:kwarpdrivekey]?<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">engaged</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Disabled</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">; Self.warpFactorLabel.text</span></span>=<span style="color: #000000;"><span style="color: #000000;">[[defaults objectforkey:kwarpfactorkey] stringvalue]; Self.favoriteTeaLabel.text</span></span>=<span style="color: #000000;"><span style="color: #000000;">[defaults objectforkey:kfavoriteteakey]; Self.favoriteCaptainLabel.text</span></span>=<span style="color: #000000;"><span style="color: #000000;">[defaults objectforkey:kfavoritecaptionkey]; Self.favoriteGadgetLabel.text</span></span>=<span style="color: #000000;"><span style="color: #000000;">[defaults objectforkey:kfavoritegadgetkey]; Self.favoriteAlienLabel.text</span></span>=<span style="color: #000000;"><span style="color: #000000;">[defaults objectforkey:kfavoritealienkey];}</span></span></pre><pre><pre><span style="color: #008000;">//</span> <span style="color: #008000;">secondviewcontroller.m</span>-(<span style="color: #0000ff;">void</span><span style="color: #000000;">) viewdidload { [super viewdidload]; </span> <span style="color: #008000;">//</span> <span style="color: #008000;">additional setup after loading the view, typically from a nib.</span> <span style="color: #000000;"> [self refreshfields];}</span> -(<span style="color: #0000ff;">void</span><span style="color: #000000;">) refreshfields{ </span>*defaults =<span style="color: #000000;"> [nsuserdefaults standarduserdefaults]; </span> =<span style="color: #000000;"> [defaults boolforkey:kwarpdrivekey]; </span> =<span style="color: #000000;"> [defaults floatforkey:kwarpfactorkey];}</span></pre></pre>Adding control response Events<p><p>Adds a listener event for the Uibutton,uiswitch and UISlider controls in Secondviewcontroller.</p></p><pre><pre>-(<span style="color: #0000ff;">void</span><span style="color: #000000;">) engineswitchtapped { </span>*defaults =<span style="color: #000000;"> [nsuserdefaults standarduserdefaults]; [defaults SetBool:self.engineSwitch.on forkey:kwarpdrivekey]; [defaults synchronize];}</span> -(<span style="color: #0000ff;">void</span><span style="color: #000000;">) warpslidertouched { </span>*defaults =<span style="color: #000000;"> [nsuserdefaults standarduserdefaults]; [defaults SetFloat:self.warpFactorSlider.value forkey:kwarpfactorkey]; [defaults synchronize];} Switch to Settings application</span> -(<span style="color: #0000ff;">void</span><span style="color: #000000;">) settinginfoclicked{ [[uiapplication sharedapplication] openurl:[ Nsurl urlwithstring:uiapplicationopensettingsurlstring]];}</span></pre></pre>Registering default values<pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">APPDELEGATE.M</span></span>-(BOOL) application: (uiapplication *) application didfinishlaunchingwithoptions: (nsdictionary *<span style="color: #000000;"><span style="color: #000000;">) launchoptions {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Override point for customization after application Launch. </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Some custom settings after the program starts</span></span>Nsdictionary *defaults =<span style="color: #000000;"><span style="color: #000000;">@{kwarpdrivekey: @YES, kwarpfactorkey:@</span></span><span style="color: #800080;"><span style="color: #800080;">2</span></span><span style="color: #000000;"><span style="color: #000000;">, Kfavoritealienkey:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Vulcan</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;"> }; [[nsuserdefaults standarduserdefaults] registerdefaults:defaults]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">YES;}</span></span></pre>Guaranteed settings are valid<p><p>If you now run the app, look at the settings information and press the home button to modify some Values. Then press the home key again to reopen the app, and the results find that the settings are not in Effect.</p></p><p><p>The reason: in ios, when the app is running, pressing the home key doesn't quit the app, but the operating system pauses it in the background so it can start quickly at any Time.</p></p><p><p>In this example, we need to add a bit of work to enable the app to be woken up to reload user preferences and display them again.</p></p><p><p>notification, a lightweight mechanism for communicating between objects. Any object can define one or more notifications sent to the app notification hub. A notification hub is a singleton object that transmits notifications between objects.</p></p><p><p>The UIApplication class sends a lot of notifications, and the use of most notifications can be seen in the name, which we use in this Example.</p></p><p><p>Add the following code to the two Controller's viewdidload method:</p></p><pre><pre>UIApplication *app =<span style="color: #000000;"> [uiapplication sharedapplication]; </span> <span style="color: #0000ff;">Object</span>: app];</pre></pre><p><p>Then add the appropriate response method</p></p><pre><pre>-(<span style="color: #0000ff;">void</span>) applicationwillenterforeground: (nsnotification *<span style="color: #000000;">) notification{ </span>*defaults =<span style="color: #000000;"> [ Nsuserdefaults standarduserdefaults]; [defaults synchronize]; [self refreshfields];}</span></pre></pre>Summary<p><p>This article explains how to use app settings, how to read preferences using nsuserdefaults, and how to enable users to modify preferences within the app.</p></p><p><p></p></p><p><p>Instance source code address: Https://github.com/CharsDavy/BridgeControl</p></p><p><p></p></p><p><p>iOS app settings</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.