I have an IOS app with multiple UIIMageView objects and wondering what would be the best way to remember their locations if the user killed the app or if they wanted to save that particular layout for future use.
I'm a beginner at this so looking for the simplest and most effective method. The only thing I can think of at the moment is to extract CGPoint location and tag of each object and save to NSUserDefaults or something but i'm hoping theres an easier way...
For something as simple as storing CGPoints, I'd recommend using NSUserDefaults, because you can very easy manage the data in it using a NSMutableArray.
You can go for Core Data and then maybe integrate iCloud, however NSUserDefaults keeps it simple.
Since your problem is to do with the state of the app, I'd recommend reading what Apple has to say about it.
Related
I'm a brand-new iOS developer and have no previous experience with Objective-C, though I'm familiar with the C programming language and the Object-Oriented Programming paradigm.
To begin I followed the Start Developing iOS Apps Today tutorial and developed a basic to-do list app where each item is a NSMutableString object stored in a NSMutableArray. My app is working properly. However, my to-do list disappear when I quit and relaunch the app on the simulator. Obviously this happens because I provided no way to make the array persist over time.
Here there's a solution to this problem using NSUserDefaults, but I've seen that it's not an efficient solution, mostly because NSUserDefaults is made specifically for storing user's preferences and has very poor performance. Therefore I'm searching for another, still simple and straightforward solution to make my app more professional. Despite this fact, I'm having some trouble in understanding where (i.e. in which classes and methods) to put the code to achieve that.
Thank you in advance.
To make the data persist over time, I suggest you check out NSCoding and also CoreData. Both of them are simple to use and implement.
NSCoding example
CoreData example
Even if there are solutions like CoreData that are specific to solve this problem, you might want something more light and quick to implement, but still cleaner than saving junk in the User Defaults.
NSMutableArray is NSCoding compliant, so as long as the items it contains are NSCoding compliant as well, you can easily store it in a file and retreive it back with the methods
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag
- (NSArray *)initWithContentsOfFile:(NSString *)aPath
Bear in mind that you might have problems if you're storing primitive types like boolean values, so I advice to save them with an NSNumber instead.
I'm working on a new iOS game and would like to save the progress online through iCloud. The saving is fairly frequent, but they are mostly doubles and ints with scores, unlocks and NSDate numbers.
Since internet will be required to play the game, I would like to save the game only online, not locally. When the app opens it should download the saved game from the server, and when it's done, launch the game.
Online currency will be sold through IAP, so I want to make sure the progress will not be lost even if the game is deleted and reinstalled, and of course, be available on multiple devices from the same user.
What's the best way to do this? Is it possible through iCloud? Key value seems like a mess because it doesn't sync quickly with iCloud. So Core Data?
I'm not looking for code to copy and paste, but I would like to create a discussion to find out the best solution for this case, if it's iCloud, setting up my own server, etc. Everywhere I look people say different things, but so far I couldn't find the best solution.
Thanks!
In iOS 8, you might want to look at GKSavedGame — it manages saved games associated with a Game Center player and syncs them through iCloud.
Otherwise, direct use of iCloud APIs sounds reasonable for your use case. If you write a small, well-defined set of values, the ubiquitous key-value store is very easy to use. If your save games are more complicated, write out a property list or encode your custom objects with NSKeyedArchiver, then use the NSFileManager APIs for syncing the resulting files through iCloud.
Going for Core Data or CloudKit is also possible, but sounds like it's more complicated than your game needs.
Im working on a multiplayer turn based game in xcode for iphone/ipad im considering my options when it comes to where to save users details such as name / points ect. I was considering creating a plist and storing data in this also i have considered nsuserdefaults and also core data.
Basically majority of the time the user will be adding new details on every launch however this will not be the case 100% of the time.
What would you guys consider the best approach?
This really depends on multiple factors:
How much data is it going to be saved?
How fast the data needs to be loaded?
I suggest you create a serializable object, that conforms to NSCoding protocol. There is also an option to save this as JSON or like you noted, plist.
I think Core Data is too much boiler plate, if this is all you are storing. NSUserDefaults is fast enough and already prepared for you to use.
I don't think someone can give you the correct answer, I suggest you experiment with all the options and see which one is the best for you.
I am making meeting app and I need to store users data. I am not need to use sql for now because it hard to setting it, so I need place to store data, I not want store users in RAM, so where can I do it? It not necessary to cache images for now, by if I can add it in this place in future it will be great.
Here is what I believe you are asking:
I am developing a meeting app and I need to store some user data.
I dont want to use sql for because its complex to do, so I need
a place to store my data. I not want use up unnecessary RAM,
so where can I store the data? I currently dont have any need
for cacheing images but perhaps in the future.
Not using CoreData/SQL because its hard to understand is not really a good reason. It was the native built in way of data access within iOS and it is the recommended way.
This is a very broad question since we dont know what your data looks like and if there are relationships between them, how large your dataset is, etc.
If its a small enough data list maybe you can look into using a PLIST. It will be a list of values with a key to each one. If its really small you can use NSUserDefaults If its static data, perhaps storing them in a NSArray or NSDictionary
I'm just getting started on making a board game using Cocoa/Objective-C. My plan is to first get it functional as a Mac OS X app, and then port it to iOS.
I saw something in the documentation for the Core Data API that made it sound like it was the recommended way to store data persistently, and made reference to the fact that iOS apps should be able to quit and be restored in exactly the same state. I got the initial impression that I should plan to use Core Data for any variable that has to do with the game's state to support this.
But as I'm learning more it seems like my initial impression isn't correct. Core Data seems more like something intended to provide similar features as embedded SQL, and is more complicated than is required just for storing the game state persistently on disk. Is there a simpler way to support fast app restoring in iOS apps, or is Core Data the way to go?
Core Data is fantastic for storing lots of data of different types, including custom objects.
However, if you're talking about storing things like high scores for a game or other simple int, float, BOOL, NSString, NSArray data, then for iOS NSUserDefaults is a quick and easy way to go.
Core Data lets you store data as objects, so if your game state can be described with native data types, Core Data just might work for you. You'd essentially be manipulating object states.
If you're looking for something a little more lightweight, look into the NSUserDefaults API (which is the same thing that the Settings App on iOS uses).
Alternatively, you could come up with your own format and use that, serializing your own data to disk.
I'd start with NSUserDefaults.