<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Paul Solt &#187; default values</title>
	<atom:link href="http://paulsolt.com/tag/default-values/feed/" rel="self" type="application/rss+xml" />
	<link>http://paulsolt.com</link>
	<description>Putting the Inc back in Solt since 2005.</description>
	<lastBuildDate>Wed, 12 May 2010 19:34:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>iPhone Default User Settings Null?</title>
		<link>http://paulsolt.com/2009/06/iphone-default-user-settings-null/</link>
		<comments>http://paulsolt.com/2009/06/iphone-default-user-settings-null/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 03:30:19 +0000</pubDate>
		<dc:creator>Paul Solt</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[default values]]></category>

		<guid isPermaLink="false">http://paulsolt.com/?p=232</guid>
		<description><![CDATA[I wanted to set default values for my application using a Settings.bundle and I ran into an interesting issue with iPhone SDK 2.2. If you don&#8217;t run the Settings application before your application runs for the first time, then the default settings are not set. It turns out that you&#8217;ll need to manually set them [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted to set default values for my application using a Settings.bundle and I ran into an interesting issue with iPhone SDK 2.2. If you don&#8217;t run the Settings application before your application runs for the first time, then the default settings are not set. It turns out that you&#8217;ll need to manually set them on the first time the application is run.</p>
<h4>Setting defaults to the default value&#8230;</h4>
<p>I&#8217;m not sure why there isn&#8217;t a function that can set the values for me, but after digging I found someone who ran into the same dilema. I don&#8217;t want to have multiple locations with default values (code and a Settings.bundle), so I found a programmatic way to set all the default values if they haven&#8217;t been set.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>registerDefaultsFromSettingsBundle <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>settingsBundle <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span> pathForResource<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Settings&quot;</span> ofType<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;bundle&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>settingsBundle<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Could not find Settings.bundle&quot;</span><span style="color: #002200;">&#41;</span>;
        <span style="color: #a61390;">return</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>settings <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithContentsOfFile<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>settingsBundle stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Root.plist&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>preferences <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>settings objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;PreferenceSpecifiers&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #400080;">NSMutableDictionary</span> <span style="color: #002200;">*</span>defaultsToRegister <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableDictionary</span> alloc<span style="color: #002200;">&#93;</span> initWithCapacity<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>preferences count<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>prefSpecification <span style="color: #a61390;">in</span> preferences<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>key <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>prefSpecification objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Key&quot;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>key<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            <span style="color: #002200;">&#91;</span>defaultsToRegister setObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>prefSpecification objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;DefaultValue&quot;</span><span style="color: #002200;">&#93;</span> forKey<span style="color: #002200;">:</span>key<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSUserDefaults</span> standardUserDefaults<span style="color: #002200;">&#93;</span> registerDefaults<span style="color: #002200;">:</span>defaultsToRegister<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>defaultsToRegister release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>All you have to do is call the above function if one of your Standard User Defaults returns <strong>null</strong>. Make the call once your application finishes loading like so:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>applicationDidFinishLaunching<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application <span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// Get the application user default values</span>
	<span style="color: #400080;">NSUserDefaults</span> <span style="color: #002200;">*</span>user <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSUserDefaults</span> standardUserDefaults<span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>server <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>user stringForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;server_address&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>server<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #11740a; font-style: italic;">// If the default value doesn't exist then we need to manually set them.</span>
		<span style="color: #002200;">&#91;</span>self registerDefaultsFromSettingsBundle<span style="color: #002200;">&#93;</span>;
		server <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSUserDefaults</span> standardUserDefaults<span style="color: #002200;">&#93;</span> stringForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;server_address&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#91;</span>window addSubview<span style="color: #002200;">:</span>viewController.view<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>window makeKeyAndVisible<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p><strong><span class="pun">References:</span></strong></p>
<ul>
<li><span class="pun"><a title="iPhone Settings" href="http://stackoverflow.com/questions/510216/can-you-make-the-settings-in-settings-bundle-default-even-if-you-dont-open-the-s" target="_blank">http://stackoverflow.com/questions/510216/can-you-make-the-settings-in-settings-bundle-default-even-if-you-dont-open-the-s</a><br />
</span></li>
<li>Display code in your WordPress blog: <a href="http://wordpress.org/extend/plugins/wp-syntax/">http://wordpress.org/extend/plugins/wp-syntax/</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://paulsolt.com/2009/06/iphone-default-user-settings-null/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->