When I sync the user data (if the user request) to iCloud (using NSUbiquitousKeyValueStore),
it syncs correctly. However, when I use 2 different devices, with 2 different apple IDs I see the SAME data on both and I'm still able to sync across them. So multiple users see each others data.
Is there any way to make the syncing user specific or have I made a mistake somewhere?
Some code that I use:
NSUbiquitousKeyValueStore *icloudatas = [NSUbiquitousKeyValueStore defaultStore];
//save to iCloud
[icloudatas setString:[NSString stringWithFormat:#"%i",
[defaults integerForKey:#"lvl"]] forKey:#"lvl"];
//load from iCloud
[defaults setInteger:[[icloudatas stringForKey:#"lvl"] integerValue] forKey:#"lvl"];
[defaults syncronise];
[icloudatas synchronize];
Problem resolved:
I just changed my Apple ID int the App Store, I forgot to change in iCloud.
That resolved my problem, everything is working properly.
You shouldn't be copying the details into NSUserDefaults inside the app. This is how you are mixing up details from different users. Just use NSUbiquitousKeyValueStore on its own.
Well, more realistically, you can copy data, but you shouldn't just always copy all data between both stores.
You should be observing NSUbiquityIdentityDidChangeNotification and NSUbiquitousKeyValueStoreDidChangeExternallyNotification and checking the reason for NSUbiquitousKeyValueStoreAccountChange (from NSUbiquitousKeyValueStoreChangeReasonKey). When you detect that the user account changed you need to 'reset' any local data either to default values or to cloud values (and prevent any update to cloud values before that happens).
Related
Can I save data into the realm without making a model or object file and also retrieve from it using swift?
I don't believe this is possible. Realm models define the schema for that object type, and everything going into Realm needs to have a schema.
If you haven't gone over the docs, the Realm academy page could be a good place to start: Realm Academy
It's hard to say without more information in the original question, but if you want to skip defining the model because it's a one-off piece of data, you can consider storing it in UserDefaults. But there are caveats with that, and one of them is that values stored in UserDefaults are not secure. You should never store sensitive information there. Also, UserDefaults can only store certain data types, and storing too much data in UserDefaults can slow down the launch of your app.
More info on user defaults.
trying to understand the concept for Photos/Photos.h framework.
my goal is:
write captured video url (or asset) to app's "userDefaults".
read from "userDefaults", & fetch each saved asset data (thumbnail & url)
Since you're not providing any code (nor asking for any), I can help sort some of this out for you -- but you need to study a bit more before you can put it all together. Especially if you think you've asked a question which has one simple correct answer.
UserDefaults is not a good place to store an image. Images are big. (You should look at Apple's documentation of what UserDefaults is for/how it's intended use).
UserDefaults
There's more than one place to store images. Do you want the system to delete them if you start running out of memory? Then it belongs in cache:
let cachesPath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).last!
Do you want to depend on it being around the next time the app is run? There is a standard place for that as well:
let userDocumentsFolder = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
Do you want iTunes to back up the images for you automatically? It expects files to be in a certain place for automatic backup. Do you have a way to keep track of where it is (the path to the file can change if the app is re-run). For that you might require persistent storage, so CoreData or Realm might be an option for you. Or you could scan your directory and create a list of files you've already saved. Then you'll need a way to select the correct one. (What did you call it? Should the user select it?)
Apple has very clearly written and useful documentation on access to the Photos library and using PHAssets. Here's just one example:
PHAsset - Photos
There are a lot of talented people on this site, and they are willing to help you, but you need to do your homework before coming here.
I recommend you read these linked documents, start writing some code, and if you run into problems please come back and ask any specific question you have about any specific problem you've encountered. Include the code which causes the problem, as well as the exact error message you are getting. We will be glad to help.
I am currently storing and retrieving an NSArray in an "old school" fashion by doing encoding and decoding on the array of objects.
I want to move this storage to iCloud.
Is there a simple way to save an array of custom objects to iCloud and retrieve them?
PSEUDOCODE EXAMPLE
allocate mutable array
itereate 5 times
create custom object
add object to mutable array
end iteration
store mutable array to iCloud
...later on...
retrieve mutable array from iCloud
retrieve specific object from mutable array
Yes, you can save any type of data to iCloud, if iCloud is enabled on the device. You have a couple of options:
If the data is small, use the key-value store. This works a lot like NSUserDefaults, but it syncs via iCloud. This is the easiest way but is the most restrictive option and is limited to no more than 1MB of data.
Use the file API. Any file can be synced via iCloud by creating it locally and then using NSFileManager to make it "ubiquitous" (i.e. iCloud synced). On the receiving end you use NSMetadataQuery to locate files in iCloud, and then NSFileManager to download them and to get the latest version.
Use UIDocument, which has its own semantics for saving and syncing data via iCloud.
I am saving user info into NSUserDefaults with key 'userInfo' right now.
and when app is launched, I check if 'userInfo' exists and switch the flow.
It works very well, but is it a bad habit to directly access to NSUserDefaults
each time I need user's info ?
Is it better to save user info when app is launced to my custom class?
If so, can you tell me the reason?
Access NSUserDefaults when you need them. If you save the user data in your class - I assume you mean in memory - you are blocking valuable memory. Depending on the size of your user data that might be an issue. On the other hand NSUserDefaults access is fast and inexpensive.
I am building an alarm app using local notifications.
I want to add settings inside my app.
So for this I have created the whole view,now I am confused how to store data for multiple alarms?
Whether i should use NSUserDefaults or sqlite.
You have all the choices:
a) Use NSUserDefaults by doing some simple Key amendments like with keys like SettingAttr = Value, e.g. "MyAlarm0Start" = 10:23:12, "MyAlarm1Start" = ... and for instance the number "MyAlarmCount" = 2
or use
b) a complete data model by CoreDate or SqlLite.
I think this strongly depends on how much data you want to store.
Concerning iTunes library am do not understand what you wanted to know.
Edit: Picking things from your media library is handled via the "Media Player Framework", see Apple Doc IPod Library Access.