I have an iOS application that uses the keychain to store some information related to the authentication. I would like to remove this data from the keychain when the application is uninstalled.
How can I get aware of the application being uninstalled/removed?
You canĀ“t detect when your application is going to be deleted from your device. But you could detect when your app is launched for first time, after to be installed. For this I use the next code:
if (![[NSUserDefaults standardUserDefaults] boolForKey:#"HasLaunched"])
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"HasLaunched"];
[[NSUserDefaults standardUserDefaults] synchronize];
//Here delete your info kept in your keychain. If not exist will not delete nothing, but if it exist mean your app has been installed again.
[removeYourPropertyInKeychain];
}
Related
iOS application written in Objective-C only show English & Emoji keyboards.
All other application are working normally and other keyboards (ex: Polish, Hebrew) are available to use.
I tried to look for .plist restrictions or something i did in the development process. didn't find anything.
appreciate your help.
Well, i found the problem.
In my main.m file i had this two rows that prevent my system from allowing any other keyboard then english:
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:#"en_EN", nil] forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize]; //to make the change immediate
To overwrite this code for users who already installed the applkaction - i have changed it to:
if ([[NSUserDefaults standardUserDefaults]valueForKey:#"AppleLanguages"]){
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize]; //to make the change immediate
}
Now everything is working as expected.
After successfully login with services call, I am showing dashboard into my application, there we have another service calls.
Problem is after running time application and then killing the app thread the link between app and services call is collapsed.
How to maintain that user is already logged if he/she login into application and kills the app. (Killing app double click home button and swipe up the app).
Do I have to call same login services on application did finished launch.
or check the user already login
My Question here is maintaining Session for accessing other services. But from server side its always says user is not login In after killing app from back ground.
#All
Your info helpful in make for integration services.
Thanks In Advance.
You can set a BOOL value to NSNumber object and add it to NSUserDefault when the login finish.
NSUserDefaults *boolUserDefaults = [NSUserDefaults standardUserDefaults];
[boolUserDefaults setObject:[NSNumber numberWithBool:YES]
forKey:#"loginStatus"];
Later you'll be able to retrieve that value as plain BOOL using -boolForKey: function in NSUserDefaults and based on that you can check if a user is already logged in.
Also you can maintain in Local database, coredata etc based on your requirement.
UPDATE
If you want to maintain the session, You can ask the server team to provide you an accesstoken on the login API response & save that as a String in NSUserDefaults and pass that string in your service calls.
If login is success, have below.
[[NSUserDefaults standardDefaults] setValue:#"yes" forKey:#"isLoggedIn"];
[[NSUserDefaults standardDefaults] synchronize];
For checking use below.
if ([[[NSUserDefaults standardDefaults] valueForKey:#"isLoggedIn"] isEqualToString:#"yes") {
NSLog(#"You are logged in");
} else {
NSLog(#"You are not logged in");
}
I am not sure about your server implementation. Usually, when logged in, the client(mobile app) will receive a token. This token should be included in the requests made afterward to show to the server that this user is logged in.
The http header is a good place to start to check whether some token is set when the login api is returned from the server. If yes, you may need to save that token:
[[NSUserDefaults standardDefaults] setValue:token forKey:#"loginToken"];
[[NSUserDefaults standardDefaults] synchronize];
Then, if the app is killed, upon launch:
NSString *token = [[NSUserDefaults standardDefaults] valueForKey:#"loginToken"];
//set the token to http header
I am a web developer that has recently moved over to iOS dev. I have implemented a login system for my website and I want to integrate this same functionality within my app. I will have login UI in the app that communicates with the web server using the AFNetworking library to make POST requests.
However, I am not sure how I will deal with remembering things in regard to user logins such as whether the user was logged in previously? With Web, I would do cookies and sessions, but I am not sure whether this is the best approach with iOS. I've also read about some people talking about storing things within the iOS keychain for security reasons. Can anyone shine some light on what the best practise for achieving what I want would be?
Thanks!
I would use NSUserDefaults but I think you should use token-based approach for your iOS client. When ever you log in and get a successful response, you should store your token in user defaults with the following code.
[[NSUserDefaults standardUserDefaults] setValue:token forKey:#"auth_token"];
[[NSUserDefaults standardUserDefaults] synchronize];
Your logout function could look something like this.
- (void)logout {
[[NSUserDefaults standardUserDefaults] setValue:nil forKey:#"auth_token"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
You can send this auth_token each time you make a web request so that the server knows those requests are authorized.
In objective-C there is the NSUserDefaults that can be used similarly.
To save a bool value for example you would do the following
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"IsLoggedIn"];
[[NSUserDefaults standardUserDefaults] synchronize];
to later load the value you write
BOOL isLoggedIn = [[NSUserDefaults standardUserDefaults] boolForKey:#"IsLoggedIn"];
Hope it helps!
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
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.