Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-22-2009, 03:42 PM   #1 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default app can't read preference unless Settings app is run first

This is very strange. I must be doing something fundamentally wrong.

My iPhone app has some preferences, with defaults stored in Settings.bundle/Root.plist. Looks like this:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Title</key>
	<string>iDSControl</string>
	<key>StringsTable</key>
	<string>Root</string>
	<key>PreferenceSpecifiers</key>
	<array>
		<dict>
			<key>Type</key>
			<string>PSTextFieldSpecifier</string>
			<key>Title</key>
			<string>D3 IP Address</string>
			<key>Key</key>
			<string>ip_address</string>
			<key>DefaultValue</key>
			<string>192.168.1.250</string>
			<key>IsSecure</key>
			<false/>
			<key>KeyboardType</key>
			<string>NumbersAndPunctuation</string>
			<key>AutocapitalizationType</key>
			<string>None</string>
			<key>AutocorrectionType</key>
			<string>No</string>
		</dict>
	</array>
</dict>
</plist>
The code to read the preference is:

Code:
#define kIPAddressKey	@"ip_address"
- (void)applicationDidFinishLaunching:(UIApplication *)application {    
	
	NSString *ipAddress = [[NSUserDefaults standardUserDefaults] stringForKey:kIPAddressKey];
	// do something with ipAddress here....
	
    [window makeKeyAndVisible];
}
When I first install this on the iPhone, the application crashes because ipAddress is NULL. If I switch to the Settings app and look at the page for this app, the default value from the Settings.bundle is there. When I switch back to the app, it runs fine. So apparently my app isn't getting the defaults until after Settings is run once. Any ideas on how to force it to update? I guess I could look for the null value, and throw an alert, but that's not very friendly. At best, it should use the default I defined. At worst, I'd like to know how to switch over to the Settings app.

Thanks!

joe
FlyingDiver is offline   Reply With Quote
Old 03-22-2009, 05:06 PM   #2 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default

Now I'm thinking that the problem is related to my ViewController. The defaults setup (above) is done in the AppDelegate, but I have ViewControllers that are invoking methods that depend on the setup in the AppDelegate to be completed.

So it seems I need some way to tell the ViewControllers that the App setup is done, rather than them doing stuff in viewDidLoad. Do I need to get the controller list from the rootController and iterate through it, sending some sort of "startNow" message?

joe
FlyingDiver is offline   Reply With Quote
Old 03-22-2009, 05:46 PM   #3 (permalink)
Tutorial Author
 
Join Date: May 2008
Posts: 315
Default

Unless they've changed something, you have to parse and synchronize the settings the first time the program runs; otherwise, you get the behavior that you're experiencing. There used to be an example app on the iPhone developer portal where Apple showed how to do this.
myersn024 is offline   Reply With Quote
Old 03-22-2009, 06:30 PM   #4 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default

Quote:
Originally Posted by myersn024 View Post
Unless they've changed something, you have to parse and synchronize the settings the first time the program runs; otherwise, you get the behavior that you're experiencing. There used to be an example app on the iPhone developer portal where Apple showed how to do this.
Interesting. None of the books or examples I saw ever pointed that out. I guess the Settings app does that for you, whenever it runs. Doesn't seem optimal to make me write parsing code that's already in Settings.

In any case, that fixed it. Thanks!

joe
FlyingDiver is offline   Reply With Quote
Old 06-19-2009, 04:41 AM   #5 (permalink)
New Member
 
Join Date: Oct 2008
Posts: 16
Default

Quote:
Originally Posted by FlyingDiver View Post
Interesting. None of the books or examples I saw ever pointed that out. I guess the Settings app does that for you, whenever it runs. Doesn't seem optimal to make me write parsing code that's already in Settings.

In any case, that fixed it. Thanks!

joe
Hi

Im having the very same problem.
Can you illustrate how to "parse and synchronize the settings the first time the program runs"?

Thanks a lot!
Gonso
gonso75 is offline   Reply With Quote
Old 08-24-2010, 05:42 AM   #6 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 1
Default solution

Well after spending more than a day on this issue i found a simple workaround that works perfectly.
I simply check if the appDefaults returns null, if it does i set the default value (in code) and then call the appDefaults synchronize afterwards.
That solves the issue.


if ([appDefaults stringForKey:@"my_key"] == nil){
[appDefaults setObject:@"my default value" forKey:@"my_key"];

}
NSString * val = [appDefaults stringForKey:@"my_key"];
[appDefaults synchronize];

good luck guys...
menachi is offline   Reply With Quote
Old 09-06-2010, 02:19 PM   #7 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 180
Default

Quote:
Originally Posted by menachi View Post
Well after spending more than a day on this issue i found a simple workaround that works perfectly.
I simply check if the appDefaults returns null, if it does i set the default value (in code) and then call the appDefaults synchronize afterwards.
That solves the issue.


if ([appDefaults stringForKey:@"my_key"] == nil){
[appDefaults setObject:@"my default value" forKey:@"my_key"];

}
NSString * val = [appDefaults stringForKey:@"my_key"];
[appDefaults synchronize];

good luck guys...
Have you figured out why the values are not read in first time??
shnaps is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 266
20 members and 246 guests
14DEV, @sandris, ADY, ArtieFufkin10, ckgni, Dani77, HemiMG, IphoneSdk, jakerocheleau, JasonR, jimbo, MACralik, NSeven, prchn4christ, Rudy, silverwiz, spiderguy84
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,767
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:55 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0