should I save nsuserdefaults to my custom class? - objective-c

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.

Related

Storing data without making model in realm using swift

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.

PHAsset - Property way to save captured PHAssets locally? (in app)

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.

How to make iCloud data sync user specific?

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).

How to store UserID username in Titanium

My app is similar to facebook.
I want to retain the userID/Username upon login screen, so that i could use it at rest of the application for queries online data.
I tried storing it in a javascript object after successful login, but it gets washed away when i move to other screens.
Thanks
You can use the App.Properties API.
Example, right from the docs.
Titanium.App.Properties.setString("my_prop","cool");
if(Ti.Facebook.loggedIn) {
if(Ti.App.Properties.hasProperty('fbid')) {
var fbid = Ti.App.Properties.getString('fbid');
}
}
You can use the hasProperty to verify they have logged in in the past and when they do logout you can use the removeProperty so that your app assumes they haven't logged in and starts over.
If you are going to store the information in the properties, you should be aware that the is information persisted on the device.
The use of properties for short term storage IMHO should be avoided if possible.
You have two choices, one store your variables in the Titanium namespace
Titanium.App.credentials = { "user" :"username", "pwd":"password123"};
Or create your own namespace and store your variable there. This will ensure you values are available throughout the application, but not stored on the device.
There is an example on my blog here http://blog.clearlyinnovative.com/post/6519148138/titanium-appcelerator-quickie-global-variables-scopes

How can I retrieve the HTML to be loaded into a WebView (or WebFrame) from a local persistent store?

So, I have a bunch of HTML is being stored in a SQLite database, and they link back and forth amongst themselves. When a user clicks to follow a link, that request needs to be serviced by pulling the appropriate HTML out of the database. This could result in needing to load images, which are also being stored in the database (this is a future thing; there are no images yet, but I'd like to be able to use them). I've looked through the WebKit documentation, but can't quite figure out how to make this happen. I've mostly looked at WebFrameLoadDelegate and WebResourceLoadDelegate, but I didn't see one that would let me catch the request, grab the appropriate content, and then send that in a response.
Ideas? I'm pretty new to Objective-C and Cocoa, but I think I'm mostly getting the hang of things.
How do the pages which are stored in the database link to each other? It is probably easiest if they use some sort of customer URL scheme to start with.
The approach I would use is to implement
-webView:resource:willSendRequest:redirectResponse:fromDataSource:
in your resource load delegate. If the request is for a resource that is actually located in your database, return a new[1] NSURLRequest which uses a custom URL protocol which points to the database resource:
x-my-scheme:///table/row
[1] Unless you are already linking amongst your resources with the custom URL scheme - then you can skip this step.
Then, implement a custom NSURLProtocol for x-my-scheme which knows how to retrieve the data from the database. The PictureBrowser sample gives a simple example of how this is done.