I use the following code in a simple MonoTouch iPad application:
if (NSUserDefaults.StandardUserDefaults.StringForKey("spid") == null)
{
NSUserDefaults.StandardUserDefaults.SetString ("spid", "myName");
NSUserDefaults.StandardUserDefaults.SetString ("spdomain", "myDomain");
NSUserDefaults.StandardUserDefaults.SetString ("sppwd", "myPassword");
NSUserDefaults.StandardUserDefaults.Synchronize();
}
WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential(NSUserDefaults.StandardUserDefaults.StringForKey("spid"),
NSUserDefaults.StandardUserDefaults.StringForKey("sppwd"),
NSUserDefaults.StandardUserDefaults.StringForKey("spdomain"));
The intent (for now until I have a proper view built) is to provide default credentials which will be stored in the plist file (later it will be moved into the KeyChain for better security). However, the calls to StringForKey(“spid”), etc. return null rather than “myName”.
Why are my default values not persisted after the call to Synchronize()? Even adding a Synchronize() call after each SetString() did not solve the issue.
The parameters for
SetStringfollow the Apple API (and convention, which is rather confusing for .NET developers, me included) that puts the first parameter has the value and the second parameter as the name.So you need to write:
to get the right behaviour.