Store NSUserDefaults and iCloud data at the same time.
The documentation for NSUbiquitousKeyValueStore says:
Rather than use the key-value store as the sole source of your data, you should instead use it as a mechanism for updating a locally stored set of configuration values.
That's what SDCloudUserDefaults does. Use it instead of NSUserDefaults for anything you want mirrored in iCloud. Best to also register for notifications when the app launches or you might lose updates.
Note: By popular request I have enabled OS X support in the CocoaPods configuration. This should work but I have not been able to test it. Please let me know if you get it working. Patches are welcomed if changes are required.
There are four ways of adding
SDCloudUserDefaults to your project:
Add the following line to your Podfile:
There is no step two.
Add the following line to your Cartfile:
And follow the steps here to add the compiled framework tot your project
If you're using iOS 5 or above and are happy with ARC, it's as simple as:
Copy SDCloudUserDefaults .h and SDCloudUserDefaults.m into your project.
NSUserDefaults or the iCloud equivalents, all of
are class methods, so there's no need to get the
(I'm a lazy and bad typist).
Most of the methods are pretty obvious. For example
+(NSString*)stringForKey:(NSString*)aKey; +(void)setString:(NSString*)aString forKey:(NSString*)aKey; +(void)removeObjectForKey:(NSString*)aKey;
There are similar methods for a few of the other data types (bool, object,
integer). As with
NSUserDefaults there is also a
synchronize to make sure
your changes are committed -- locally at least, it's not possible to guarantee
that iCloud has seen them.
If you want to import changes from iCloud (and you probably do), you need to call this method somewhere near the start of your app:
/* * Copyright 2011-2015 Wandle Software Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */