I am writing a first iOS app.. It's just getting user input and sending it to customized API.
However, when the user is in some black spot (no signal), I want the app to save the data into local and automatically send it when network becomes available..
What is the best and simple way to achieve this?
Use Reachability to detect whether the user has an Internet connection or not. If they do, send the data normally. If they don't have an Internet connection, then save the data into the Documents Directory or using NSUserDefaults.
[[NSUserDefaults standardUserDefaults] setObject:object forKey:#"Data1"];
NSString *string = [[NSUserDefaults standardUserDefaults] objectForKey:#"Data1"];
Related
I'm writing a feature of the language change. First screen of the app I'm writing is about setting the language. When he user presses the button, my app directs user to app settings. I expect user to set some language and show it to them as soon as they go back to my app.
But as soon as I click on some language option, my app xcode console says:
Message from debugger: Terminated due to signal 9
Additionally, as soon as I return to my app, it gets loaded as if I closed and reopened it.
I thought that maybe I can have some code that will set a global variables like "currentLanguage" and "previousLanguage" and if those are different - I will load localisation files for "currentLanguage" and will start downloading localised videos from my server.
System terminate your app when user change language in Settings. So you can use NSUserDefaults for persistent storage and compare this values on app start, to detect if language was changed.
Example:
NSString *currentLanguage = [[NSLocale preferredLanguages] firstObject];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject: currentLanguage forKey:#"previousLanguage"];
NSString *previousLanguage = [userDefaults objectForKey:"previousLanguage"];
I am developing an app that will eventually interact with a website through a web service that supports many users. The app user will then need a userid and password to synch up data. For an url connection to a server that requires authentication, answers on SO suggest that nsurl credentials or keychain are the way to go. However, initially, I just want to give the user and app a userid--that is persistent but can be changed when desired--so that I can do things like incorporate the userid in profile picture names and so forth following the format used in the web app.
Ideally, I'd like to do this now in a way that I don't have to repeat work later.
Is there a best practice for storing a user's userid in an app that I could do now prior to actually connecting to any web services?
Thanks for any suggestions.
You can use the NSUserDefaults to save persistent user data
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:#"12234" forKey:#"userID"];
[userDefaults synchronize];
You can then get the User ID back by using
NSString* userID = (NSString *) [userDefaults objectForKey:#"userID"];
Class Reference:
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/index.html
Is there a way to get the date when the user downloaded / installed the first time an app via Facebook ? In my app , in the first launch you have to connect with Facebook. So I wonder , is there a way to get the date of this first connection.
I found nothing interesting during my research on internet..
Thanks !
Here is multiple way to do this.
1.> You can store it on server end along with device UUID at login time.When you login first time with facebook via a remote connection , at server end you can put a check that this UUID is aready exist or not. If it is not then you can insert this UUID along with current date and time into server end database.
2.> You can store it locally in NSUserDefaults . Whenever you login with facebook , just check NSUserDefaults value via this code
if (![[NSUserDefaults standardUserDefaults] valueForKey:#"firstTimeFaceBookLogin"])
{
[[NSUserDefaults standardUserDefaults] setValue:[NSDate date] forKey:#"firstTimeFaceBookLogin"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
NSLog(#"current date ===%#",[[NSUserDefaults standardUserDefaults] valueForKey:#"firstTimeFaceBookLogin"]);
Now NSUserDefaults value any where in your app via firstTimeFaceBookLogin key. But if you delete your application from device then this NSUserDefaults would be clear and again create a store new value after installing it in your device.
I hope you understand it. Please let me inform if you still having any confusion. Thanks
We are developing an iPhone application. The app is available in the app store. We are using NSUserDefaults to store our usertoken value. But some users reports that the usertoken is became null when the app idle (in background) for long time. But normally the value is getting.
The following is the code for storing usertoken in the NSUserDefaults. We are setting the value to the userToken from the login page and signup pages.
[[NSUserDefaults standardUserDefaults] setObject:userToken forKey:USER_TOEKN];
I have not called synchronize after setting value to NSUserDefaults. Could you please help
You need to call [[NSUserDefaults standardUserDefaults] synchronize]; if your app is going to be backgrounded - my guess is the app idles before the defaults automatically synchronizes. From the documentation:
Because this method is automatically invoked at periodic intervals, use this method only if you cannot wait for the automatic synchronization (for example, if your application is about to exit) or if you want to update the user defaults to what is on disk even though you have not made any changes.
As for where to do this, take a look at applicationWillResignActive: and applicationDidEnterBackground: in your Application Delegate.
Try something like this:
NSUserDefaults *udf = [NSUserDefaults standardUserDefaults];
[udf setObject:userToken forKey:USER_TOEKN];
[udf synchronize];
It will store your value permanently.
In iOS4, when I save NSUserDefaults, the information gets saved. But then, if the application becomes inactive, and I kill the application (by double clicking the home button and then terminating the app), and launch the application again, the NSUserDefaults are not read.
Now, is this the default behaviour of NSUserDefaults that if the app is terminated due to any of the issues, the next launch will not store the information?
The call I am using to persist the info is:-
[[NSUserDefaults standardUserDefaults] registerDefaults:
[NSDictionary dictionaryWithObjectsAndKeys:
"Some date", #"Validity",
nil]];
after you register defaults add this
[[NSUserDefaults standardUserDefaults] synchronize];
until you synchoronize, the standardUserDefaults gives current/correct values as long as the app is alive.
once the app is terminated, since you didn't sync the standardUserDefaults, the app will read the unsynced values on the next launch.
Example: Think of it like a google doc that you are editing.
the AutoSave feature of google document is equivalent to the nsuserdefaults synchornize. If you close the browser before the changes you made to the document has been autosaved, the next time you load it up, you'll see the old content.
The call you mention to persist the user defaults, registerDefaults, does not actually persist anything to disk. What registerDefaults does is initialize the defaults to use for the application if it can't find any on the disk. You should do this on each application launch, typically in the initialize method for the app delegate.
Once your application is running it will typically modify the user defaults. Whenever you want to save these modified defaults to the disk you should call:
[[NSUserDefaults standardUserDefaults] synchronize];
After the defaults have been saved they will be loaded automatically on any subsequent application launches.