Bug 5850 - Error when trying to save NSUserDefaults.StandardUserDefaults.AsDictionary() to iCloud
Summary: Error when trying to save NSUserDefaults.StandardUserDefaults.AsDictionary() ...
Status: RESOLVED NOT_REPRODUCIBLE
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 5.2
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-06-25 15:19 UTC by Neal
Modified: 2012-06-26 10:35 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED NOT_REPRODUCIBLE

Description Neal 2012-06-25 15:19:21 UTC
Hello,

I'm trying to use iCloud to save my app preferences to be shared across devices.  I'm using the following code:

NSUbiquitousKeyValueStore.DefaultStore.SetDictionary("settings", NSUserDefaults.StandardUserDefaults.AsDictionary());

When I run the method above an exception is thrown and this occurs on random items in my NSUserDefaults...

Objective-C exception thrown.  Name: NSUnknownKeyException Reason: [<NSUbiquitousKeyValueStore 0xaa1380> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key WebKitWebSecurityEnabled.
Exception: Objective-C exception thrown.  Name: NSUnknownKeyException Reason: [<NSUbiquitousKeyValueStore 0xaa1380> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key WebKitWebSecurityEnabled.:::  at MonoTouch.Foundation.NSObject.SetValueForKey (MonoTouch.Foundation.NSObject value, MonoTouch.Foundation.NSString key) [0x00000] in <filename unknown>:0 
  at MyApp.MySettings.SaveToCloud () [0x00000] in <filename unknown>:0 

I'm not sure if there is a problem in MonoTouch and the bindings that need to be addressed or if it's something I'm doing wrong.  These particular properties I'm seeing are BOOL settings in NSUserDefaults.StandardUserDefaults

FYI
Comment 1 Rolf Bjarne Kvinge [MSFT] 2012-06-25 18:26:14 UTC
Can you include settings that cause this crash?

I tried this, but it works for me:

NSUserDefaults.StandardUserDefaults ["WebKitWebSecurityEnabled"] = NSObject.FromObject (true);
NSUbiquitousKeyValueStore.DefaultStore.SetDictionary("settings", NSUserDefaults.StandardUserDefaults.AsDictionary());
Comment 2 Neal 2012-06-25 18:44:50 UTC
Rolf,

I'm not at the office right now but trying adding a BOOL to NSUserDefaults such as:

NSUserDefaults.StandardUserDefaults.SetBool("foo", true);
NSUserDefaults.StandardUserDefaults.Synchronize();

Now try the iCloud SetDictionary and see if you get an error.  Each time I did this, by the way, the error would be on a different user default.  It was never the same key repeated.

Thanks.
Comment 3 Rolf Bjarne Kvinge [MSFT] 2012-06-26 10:25:26 UTC
I still can't reproduce any errors, and I've tried on both an iOS 5.1 device (iPad3) and in the simulator.

Maybe the StandardUserDefaults needs to contain more values for this to happen? Does your code from comment #4 fail if you put it in a new project too?
Comment 4 Neal 2012-06-26 10:35:10 UTC
Rolf,

After some sleep...the item in the error I reported above "WebKitWebSecurityEnabled" is not in my NSUserDefaults.StandardUserDefaults - i.e. it's not something I'm setting so I have no idea where this is coming from!  I would think NSUserDefaults.StandardUserDefaults would be things I set to be saved.  So maybe this is out of my hands.

Note:  I did some searching on WebKitWebSecurityEnabled and it appears that maybe since I have in-app purchasing (StoreKit) integrated these settings are getting added external to my settings and hence being added to iCloud in my attempts.  I probably need a better way to isolate out just my settings and not everything else being put into the StandardUserDefaults that I'm not aware of.

Thanks Rolf.