<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-3571266601319130414</id><updated>2011-04-21T16:17:09.191-04:00</updated><category term='iphone code'/><title type='text'>Bits Bucket</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://bitsbucket.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3571266601319130414/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://bitsbucket.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Dave Mitchell</name><uri>http://www.blogger.com/profile/00218325839475610441</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='http://1.bp.blogspot.com/_O6E065BRocY/SQDzC0GzDJI/AAAAAAAAAAM/XaMiFkCyH04/S220/Rocket.png'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3571266601319130414.post-867741215920108840</id><published>2008-10-23T21:09:00.021-04:00</published><updated>2008-10-24T00:55:47.166-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='iphone code'/><title type='text'>Default user settings for iPhone apps</title><content type='html'>&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_O6E065BRocY/SQEgTbWDMMI/AAAAAAAAAA4/Zm8cEwS-gWQ/s1600-h/SettingsIcon.png"&gt;&lt;blockquote&gt;&lt;/blockquote&gt;&lt;blockquote&gt;&lt;/blockquote&gt;&lt;blockquote&gt;&lt;/blockquote&gt;&lt;img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 200px; height: 197px;" src="http://4.bp.blogspot.com/_O6E065BRocY/SQEgTbWDMMI/AAAAAAAAAA4/Zm8cEwS-gWQ/s200/SettingsIcon.png" border="0" alt="" id="BLOGGER_PHOTO_ID_5260521357875294402" /&gt;&lt;/a&gt;The iPhone SDK allows you to define application settings and their default values in a system bundle.  There are &lt;a href="http://iPhoneIncubator.com/blog/tutorial/how-to-create-an-iphone-preferences-file"&gt;several&lt;/a&gt; &lt;a href="http://www.iphonesdkarticles.com/2008/08/application-preferences.html"&gt;good&lt;/a&gt; &lt;a href="http://icodeblog.com/2008/10/03/iphone-programming-tutorial-savingretrieving-data-using-nsuserdefaults/"&gt;tutorials&lt;/a&gt; that teach you how to do this, and you can also study Apple's &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;AppPerf&lt;/span&gt; sample code.  Unfortunately, the current iPhone OS (as of 2.2 beta) doesn't automatically initialize the settings to your defaults.  See this &lt;a href="http://discussions.apple.com/thread.jspa?messageID=7846335"&gt;Apple discussion thread&lt;/a&gt; for more details.  This sure seems like an oversight, but for now it's up to you to properly initialize the values.  &lt;div&gt;&lt;div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;AppPerf&lt;/span&gt; sample does handle the initialization case, but in a hardcoded and nonpersistent way.  My solution was to write a function that builds up a dictionary of default values by parsing the solution bundle.   Just call this method instead of &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html#//apple_ref/occ/clm/NSUserDefaults/standardUserDefaults"&gt;NSUserDefaults:standardUserDefaults&lt;/a&gt;&lt;/span&gt; and pass in the name of one of your keys.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"&gt;- (&lt;span style="color: #5c2699"&gt;NSUserDefaults&lt;/span&gt; *) loadUserSettings:(&lt;span style="color: #5c2699"&gt;NSString&lt;/span&gt; *)aKey&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"&gt;{&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #007400"&gt;&lt;span style="color: #000000"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;&lt;/span&gt;// Load user settings&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #5c2699"&gt;&lt;span style="color: #000000"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;&lt;/span&gt;NSUserDefaults&lt;span style="color: #000000"&gt; *settings = [&lt;/span&gt;NSUserDefaults&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #2e0d6e"&gt;standardUserDefaults&lt;/span&gt;&lt;span style="color: #000000"&gt;];&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;&lt;span style="color: #aa0d91"&gt;if&lt;/span&gt; (![settings &lt;span style="color: #2e0d6e"&gt;stringForKey&lt;/span&gt;:aKey])&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;{&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #007400"&gt;&lt;span style="color: #000000"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;&lt;/span&gt;// The settings haven't been initialized, so manually init them based&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #007400"&gt;&lt;span style="color: #000000"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;&lt;/span&gt;// the contents of the the settings bundle&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;&lt;span style="color: #5c2699"&gt;NSString&lt;/span&gt; *bundle = [[[&lt;span style="color: #5c2699"&gt;NSBundle&lt;/span&gt; &lt;span style="color: #2e0d6e"&gt;mainBundle&lt;/span&gt;] &lt;span style="color: #2e0d6e"&gt;bundlePath&lt;/span&gt;]&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #2e0d6e"&gt;&lt;span style="color: #000000"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;     &lt;/span&gt;&lt;/span&gt;stringByAppendingPathComponent&lt;span style="color: #000000"&gt;:&lt;/span&gt;&lt;span style="color: #c41a16"&gt;@"Settings.bundle/Root.plist"&lt;/span&gt;&lt;span style="color: #000000"&gt;];&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #2e0d6e"&gt;&lt;span style="color: #000000"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #5c2699"&gt;NSDictionary&lt;/span&gt;&lt;span style="color: #000000"&gt; *plist = [[&lt;/span&gt;&lt;span style="color: #5c2699"&gt;NSDictionary&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;dictionaryWithContentsOfFile&lt;span style="color: #000000"&gt;:bundle]&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #c41a16"&gt;&lt;span style="color: #000000"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #2e0d6e"&gt;objectForKey&lt;/span&gt;&lt;span style="color: #000000"&gt;:&lt;/span&gt;@"PreferenceSpecifiers"&lt;span style="color: #000000"&gt;];&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #5c2699"&gt;&lt;span style="color: #000000"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;&lt;/span&gt;NSMutableDictionary&lt;span style="color: #000000"&gt; *defaults = [&lt;/span&gt;NSMutableDictionary&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #aa0d91"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt;];&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #007400"&gt;&lt;span style="color: #000000"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;&lt;/span&gt;// Loop through the bundle settings preferences and pull out the key/default pairs&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;&lt;span style="color: #aa0d91"&gt;for&lt;/span&gt; (&lt;span style="color: #5c2699"&gt;NSDictionary&lt;/span&gt;* setting &lt;span style="color: #aa0d91"&gt;in&lt;/span&gt; plist)&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;{&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;   &lt;/span&gt;&lt;span style="color: #5c2699"&gt;NSString&lt;/span&gt; *key = [setting &lt;span style="color: #2e0d6e"&gt;objectForKey&lt;/span&gt;:&lt;span style="color: #c41a16"&gt;@"Key"&lt;/span&gt;];&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;   &lt;/span&gt;&lt;span style="color: #aa0d91"&gt;if&lt;/span&gt; (key)&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;    &lt;/span&gt;[defaults &lt;span style="color: #2e0d6e"&gt;setObject&lt;/span&gt;:[setting &lt;span style="color: #2e0d6e"&gt;objectForKey&lt;/span&gt;:&lt;span style="color: #c41a16"&gt;@"DefaultValue"&lt;/span&gt;] &lt;span style="color: #2e0d6e"&gt;forKey&lt;/span&gt;:key];&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;}&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #007400"&gt;&lt;span style="color: #000000"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;&lt;/span&gt;// Persist the newly initialized default settings and reload them&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #2e0d6e"&gt;&lt;span style="color: #000000"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;[settings &lt;/span&gt;setPersistentDomain&lt;span style="color: #000000"&gt;:defaults &lt;/span&gt;forName&lt;span style="color: #000000"&gt;:[[&lt;/span&gt;&lt;span style="color: #5c2699"&gt;NSBundle&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;mainBundle&lt;span style="color: #000000"&gt;] &lt;/span&gt;bundleIdentifier&lt;span style="color: #000000"&gt;]];&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #2e0d6e"&gt;&lt;span style="color: #000000"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;settings = [&lt;/span&gt;&lt;span style="color: #5c2699"&gt;NSUserDefaults&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;standardUserDefaults&lt;span style="color: #000000"&gt;];&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;}&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;&lt;span style="color: #aa0d91"&gt;return&lt;/span&gt; settings;&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"&gt;}&lt;/p&gt; &lt;p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px"&gt;&lt;br /&gt;&lt;/p&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3571266601319130414-867741215920108840?l=bitsbucket.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bitsbucket.blogspot.com/feeds/867741215920108840/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://bitsbucket.blogspot.com/2008/10/default-user-settings-for-iphone-apps.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3571266601319130414/posts/default/867741215920108840'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3571266601319130414/posts/default/867741215920108840'/><link rel='alternate' type='text/html' href='http://bitsbucket.blogspot.com/2008/10/default-user-settings-for-iphone-apps.html' title='Default user settings for iPhone apps'/><author><name>Dave Mitchell</name><uri>http://www.blogger.com/profile/00218325839475610441</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='http://1.bp.blogspot.com/_O6E065BRocY/SQDzC0GzDJI/AAAAAAAAAAM/XaMiFkCyH04/S220/Rocket.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_O6E065BRocY/SQEgTbWDMMI/AAAAAAAAAA4/Zm8cEwS-gWQ/s72-c/SettingsIcon.png' height='72' width='72'/><thr:total>0</thr:total></entry></feed>
