objective c - Unable to read values from Settings.bundle -
this first post hello , please bear me should make blunders in regard regarded etiquette here. i've been passively using site time , have read faq hope able keep these @ minimum.
i've question settings.bundle files in objective-c. i've been using following guides implement 1 of those:
i'm using both xcode 4.1 , latest xcode 3. used version 3 create settings.bundle , plist found xcode 4's plist editor buggy.
these contents of root.plist:
<?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>stringstable</key> <string>root</string> <key>preferencespecifiers</key> <array> <dict> <key>type</key> <string>psgroupspecifier</string> <key>title</key> <string>authentication</string> </dict> <dict> <key>type</key> <string>pstextfieldspecifier</string> <key>title</key> <string>user id</string> <key>key</key> <string>user_id_key</string> <key>defaultvalue</key> <string>1234</string> <key>issecure</key> <false/> <key>keyboardtype</key> <string>alphabet</string> <key>autocapitalizationtype</key> <string>none</string> <key>autocorrectiontype</key> <string>no</string> </dict> </array> </dict> </plist>
i can access settings in ios settings menu without problem , change value of single entry have far. value gets stored between sessions on ios simulator on physical ios device (an iphone 4).
i seem unable access value code. let me show how tried implement (in viewcontroller.m of app):
- (void)viewdidload { [super viewdidload]; mydefaults = [nsuserdefaults standarduserdefaults]; // debug userid = [mydefaults stringforkey:@"user_id_key"]; // userid = [nsstring stringwithstring:@"1234"]; // debug jsonbackendurl = @"http://example.com/jsonbackend.php"; passphrase = @"secretpassphrase"; theurlconnectioncontroller = [[urlconnectioncontroller alloc] initwithcallerobject:self]; [theurlconnectioncontroller getconnectionandupdate:no]; }
strangely enough when try run app in xcode 3 neither error messages (like sigabrt or such) nor debugger output. screen of iphone simulator goes black moment trying start app , i'm being taken home screen.
xcode 4 shows rather odd behavior. manages show ui of application second or 2 , takes me home screen of iphone simulator. indicates sigabrt way farther down in code try value json response:
nsstring *arrivaltoday = [nsstring stringwithstring:[jsonresponse objectforkey:@"from"]];
as json string generated php script using parameters passed in app - 1 of these user id settings.bundle - of little wonder shouldn't work. shouldn't crash catch error earlier in code that's not connected settings problem. real pain in neck though fact xcode (kind of) crashes along app in iphone simulator. first, xcode keeps working except fact can't abort program still appears run. can click stop button in xcode no effect. when try exit xcode asks me if should terminate running program , when click yes crashes real , have force quit it.
fortunately though, xcode 4 gives me call stack of app:
2011-07-21 11:41:04.539 oneclickcheckin[2098:b303] http://example.com/json.php?user=(null)&pass=secretpassphrase&getstarttoday 2011-07-21 11:41:04.888 oneclickcheckin[2098:b303] *** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '*** -[nsplaceholderstring initwithstring:]: nil argument' *** call stack @ first throw: ( 0 corefoundation 0x00de25a9 __exceptionpreprocess + 185 1 libobjc.a.dylib 0x00f36313 objc_exception_throw + 44 2 corefoundation 0x00d9aef8 +[nsexception raise:format:arguments:] + 136 3 corefoundation 0x00d9ae6a +[nsexception raise:format:] + 58 4 foundation 0x007ca14c -[nsplaceholderstring initwithstring:] + 105 5 foundation 0x007d3266 +[nsstring stringwithstring:] + 72 6 oneclickcheckin 0x00002b5c -[oneclickcheckinviewcontroller updateinterfacewithjsonresponse:] + 172 7 oneclickcheckin 0x0000e82c -[urlconnectioncontrollerdelegate connectiondidfinishloading:] + 364 8 foundation 0x007de112 -[nsurlconnection(nsurlconnectionreallyinternal) senddidfinishloading] + 108 9 foundation 0x007de06b _nsurlconnectiondidfinishloading + 133 10 cfnetwork 0x030fa48e _zn19urlconnectionclient23_clientdidfinishloadingepns_26clientconnectioneventqueuee + 220 11 cfnetwork 0x031c56e1 _zn19urlconnectionclient26clientconnectioneventqueue33processalleventsandconsumepayloadep20xconnectioneventinfoi12xclientevent18xclienteventparamsel + 293 12 cfnetwork 0x031c59cf _zn19urlconnectionclient26clientconnectioneventqueue33processalleventsandconsumepayloadep20xconnectioneventinfoi12xclientevent18xclienteventparamsel + 1043 13 cfnetwork 0x030f0c80 _zn19urlconnectionclient13processeventsev + 100 14 cfnetwork 0x030f0acf _zn17multiplexersource7performev + 251 15 corefoundation 0x00dc38ff __cfrunloop_is_calling_out_to_a_source0_perform_function__ + 15 16 corefoundation 0x00d2188b __cfrunloopdosources0 + 571 17 corefoundation 0x00d20d86 __cfrunlooprun + 470 18 corefoundation 0x00d20840 cfrunlooprunspecific + 208 19 corefoundation 0x00d20761 cfrunloopruninmode + 97 20 graphicsservices 0x0101a1c4 gseventrunmodal + 217 21 graphicsservices 0x0101a289 gseventrun + 115 22 uikit 0x00042c93 uiapplicationmain + 1160 23 oneclickcheckin 0x00002609 main + 121 24 oneclickcheckin 0x00002585 start + 53 25 ??? 0x00000001 0x0 + 1 ) terminate called throwing exception(lldb)
that's of relevant information coming mind right now. i've done extensive research on google , have read threads on site couldn't find solution.
any appreciated! :-)
i've been able solve largely above mentioned guide: adding settings bundle iphone app.
there says:
[...] important understand until user changes value of setting nothing set. if check setting in application return nil unless set default value. add following applicationdidfinishlaunching (or didfinishlaunchingwithoptions) method:
// set application defaults nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; nsdictionary *appdefaults = [nsdictionary dictionarywithobject:@"yes" forkey:@"enablerotation"]; [defaults registerdefaults:appdefaults]; [defaults synchronize];
i wasn't aware of that. seems me values in nsdictionary have initialized in form or later changes user not going take effect either (i tried time , again change values in iphone simulator while i'd been looking solution , didn't work). might affect iphone simulator though, haven't had chance test on physical device yet. initialization code looks (in appdelegate):
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { // override point customization after application launch. nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; nsdictionary *appdefaults = [nsdictionary dictionarywithobjectsandkeys: @"1234", @"user_id_key", @"http://example.com/json.php", @"json_url_key", @"secretpassphrase", @"passphrase_key", nil]; [defaults registerdefaults:appdefaults]; [defaults synchronize]; self.window.rootviewcontroller = self.viewcontroller; [self.window makekeyandvisible]; return yes; }
with code can access nsuserdefaults values later in code:
- (void) reloadpreferences { mydefaults = [nsuserdefaults standarduserdefaults]; userid = [mydefaults objectforkey:@"user_id_key"]; jsonbackendurl = [mydefaults objectforkey:@"json_url_key"]; passphrase = [mydefaults objectforkey:@"passphrase_key"]; }
i spent time hunting down bug concerning order of key-value pairs of nsdictionary until dawned on me value comes first followed key in definition. coming perl background alien me might watch out if you're getting nil values nsuserdefaults object.
this cost me 3 days fix hope solution might come in handy some. :-)
Comments
Post a Comment