I have a program that authorize on facebook with cookies. So the modal dialog with facebook authorizing every time displaying. I want to hide it from user. Any help please..
When your login was successful you could do something like:
[[NSUserDefaults standardUserDefaults] setObject:_facebook.accessToken forKey:#"FBAccessToken"];
[[NSUserDefaults standardUserDefaults] setObject:_facebook.expirationDate forKey:#"FBExpirationDate"];
[[NSUserDefaults standardUserDefaults] synchronize];
then when your application starts up, do something similar to this:
if ( [[NSUserDefaults standardUserDefaults] objectForKey:#"FBAccessToken"] != nil ) {
_facebook.accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:#"FBAccessToken"];
_facebook.expirationDate = [[NSUserDefaults standardUserDefaults] objectForKey:#"FBExpirationDate"];
}
What this is doing is, that it saves the "cookie" of the facebook authentication and reuses it when the application is restarted.
Hope this helps.
Related
I have developed an app which is related with signing in to an account and people can also get registered in my app, since those user info will be stored in a SQLite database.
The problem is that, when the user logs out of the account and then re-logs in, the app asks the user to login again. I dont want this to happen, as it should remember the user name and password and should login automatically.
Does anyone know how to get this done?
In your AppDelegate.m file , add the NSUserDefaults method ...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if ([standardUserDefaults valueForKey:#"dbID"]==nil) {
[standardUserDefaults setObject:#"1" forKey:#"dbID"];
[standardUserDefaults synchronize];
}
}
and also you have sane the username and password save the NSUserDefaults.
and for log out functionality , add the code logout code where you want the logout the applications ...
-(IBAction)Logout :(id)sender {
[[NSUserDefaults standardUserDefaults] setObject:#"" forKey:#"dbID"];
[NSUserDefaults resetStandardUserDefaults];
}
in the login time check the dbID is null then user login with the username and password. if the dbID is not null the login the old username and password.
if u are exit the without logout the applications , then store the dbID is "1" in the NSUserDefaults and redirect the view controller and For save the user name and password in the your logon time...for save
[[NSUserDefaults standardUserDefaults] setValue:#"Your text field value" forKey:#"Username"];
[[NSUserDefaults standardUserDefaults] setValue:#"Your text field value" forKey:#"password"];
read the
NSString * _UserName = [[NSUserDefaults standardUserDefaults] stringForKey:#"Username"];
NSString * _password = [[NSUserDefaults standardUserDefaults] stringForKey:#"password"];
username and password save the NSUserDefaults is indicated the u have store the username and password in NSUserDefaults.
if the you have logout the application by button , the make the NUll username and password save the NSUserDefaults .. like
[[NSUserDefaults standardUserDefaults] setValue:#"" forKey:#"Username"];
[[NSUserDefaults standardUserDefaults] setValue:#"" forKey:#"password"];
Use NSUserDefaults. This one will work fine
NSUserDefaults *rememberDefault = [NSUserDefaults standardUserDefaults];
if ([[rememberDefault valueForKey:#"Autologin"] isEqualToString:#"Autologin_on"]) {
btnAutoLogin.selected =NO;
}else {
btnAutoLogin.selected =YES;
}
In your Button Action Method :
-(IBAction)btnOnOffSwitch:(UIButton *)sender{
if (sender.selected) {
sender.selected = NO;
}else {
sender.selected = YES;
}
}
and when you want to check use this:
NSUserDefaults *rememberDefault = [NSUserDefaults standardUserDefaults];
if([[rememberDefault valueForKey:#"Autologin"] isEqualToString:#"Autologin_on"])
{
write your code here. to push directly to your home screen. this should be checked in your login screen
}
may b this will help you.
for your condition when user successfully login first time at that time make 1 dictionary for storing username , password and user id else make 3 different nsuserdefault object for this 3 types.
and when user comes again in that login screen check that nsuser default value is empty or has some value so if the value is there than it has already logsin and u can directly redirect him to next page .. else display the login page..
here i have write only the logic not source code so u can try it out your self and learn more :)..
hope this will help you.
I need to save a variable that increments every time a user hits a button within the app. I've been trying to do that with NSUserDefaults with no success. Here's the code I'm using:
[[NSUserDefaults standardUserDefaults] setInteger:i++ forKey:#"AppCount"];
When I output this in the log, however, I keep getting 0.
Any help would be greatly appreciated.
Try adding:
[[NSUserDefaults standardUserDefaults] synchronize];
After each time you change the value of your integer.
Quoting Apple's documentation on synchronize:
Writes any modifications to the persistent domains to disk and updates
all unmodified persistent domains to what is on disk.
http://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html#//apple_ref/occ/instm/NSUserDefaults/synchronize
So with this modification, your final code should look like this:
[[NSUserDefaults standardUserDefaults] setInteger:i++ forKey:#"AppCount"];
[[NSUserDefaults standardUserDefaults] synchronize];
EDIT: Actually on second thought, give this a try:
I think the problem is using i++ assumes that the application will always be able to keep track of the count, and every time you re-enter the application i gets reset.
The following works, I just tested it.
if (![[NSUserDefaults standardUserDefaults] integerForKey:#"AppCount"]) {
[[NSUserDefaults standardUserDefaults] setInteger:i forKey:#"AppCount"];
}else{
[[NSUserDefaults standardUserDefaults] setInteger:[[NSUserDefaults standardUserDefaults] integerForKey:#"AppCount"] + 1 forKey:#"AppCount"];
}
[[NSUserDefaults standardUserDefaults] synchronize];
You can use Static NSUIInteger appCount=0;
in the function user taps increase it value appCount++;
that keeps the value in function calls within the app. after app closing you write it to file.
- (void)viewDidLoad {
[super viewDidLoad];
countFirst= [[NSUserDefaults standardUserDefaults]integerForKey:#"Counter"];
countFirst++;
}
Write this wherever you want to increment.
[[NSUserDefaults standardUserDefaults] setInteger:countFirst++ forKey:#"Counter"];
I'm making an app that changes images every time the user press on the screen. It is using an int to know then to change image.
I need help to save the int value then screen is pressed and load it then the app is loaded.
You could use the NSUserDefaults class for this:
// Write
[[NSUserDefaults standardUserDefaults] setInteger:7615 forKey:#"customIntegerValue"];
[[NSUserDefaults standardUserDefaults] synchronize];
// Read
NSLog(#"Integer = %i", [[NSUserDefaults standardUserDefaults] integerForKey:#"customIntegerValue"];
// To save in pref
int highScore = yourGameScore;
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:highScore] forKey:#"HighScore"];
[[NSUserDefaults standardUserDefaults] synchronize];
// To get saved value from pref
highScore = [[[NSUserDefaults standardUserDefaults] objectForKey:#"HighScore"] intValue ];
You can see similar post here: stackOldPost
You can save values in NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
I need to track the download of a certain iphone application. I tried a lot and found out that we could track it from the AppStore. But i need to track that from my application itself. So please help me to identify the method that fires when the application starts for the first time. Thanks.
There's no specific method that fires only on the 1st application launch. You can set a flag in user defaults on application start - so if the flag is not present then that will mean that application launched for the 1st time:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
if (![[NSUserDefaults standardDefaults] boolForKey:#"AlreadyLaunched"]){
// First launch logic
[[NSUserDefaults standardDefaults] setBool:YES forKey:#"AlreadyLaunched"];
[[NSUserDefaults standardDefaults] synchronize];
}
...
}
But i need to track that from my application itself.
No.
But if you really want to do this you could use something like this:
BOOL hasUsedSpyWareFunctions = [[NSUserDefaults standardUserDefaults] boolForKey:#"SpyWareKey"];
if (!hasUsedSpyWareFunctions) {
[self spyOnUser];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"SpyWareKey"];
}
if you are a Pro in spying you only set the key to YES if the method returned successfully (ie a network connection could be established)
There’s no such an event, at least not one that I know of. But what you want can be trivially done using NSUserDefaults. Simply check for some boolean flag and if it’s not there, it’s a first run and you can set the flag:
NSString *const AlreadyRunKey = #"already-run";
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if (![prefs boolForKey:AlreadyRunKey]) {
[prefs setBool:YES forKey:AlreadyRunKey];
[prefs synchronize];
// do whatever else you want
}
After
_facebook = [[Facebook alloc] init];
and
[_facebook authorize:kAppId permissions:_permissions delegate:self];
How can I tell whether the _facebook is (still) logged-in/valid to do further [_facebook requestWithMethodName: ...]?
Or should I just simply [_facebook authorize:...] again and again? Thanks!
I was having the same issue which i solved with the solution of another Member from here, dont know link exactly. but here's the solution
When you are logged into the facebook
[[NSUserDefaults standardUserDefaults] setObject:self.facebook.accessToken forKey:#"AccessToken"];
[[NSUserDefaults standardUserDefaults] setObject:self.facebook.expirationDate forKey:#"ExpirationDate"];
In next View Controller, where you want to use the facebook with same session
_facebook.accessToken = [[NSUserDefaults standardUserDefaults] stringForKey:#"AccessToken"];
_facebook.expirationDate = (NSDate *) [[NSUserDefaults standardUserDefaults] objectForKey:#"ExpirationDate"];
if ([_facebook isSessionValid] == NO) {
//[_facebook authorize:kAppId permissions:self.permissions delegate:self]; //Create new facebook instance
}
Let me know if i put something which does not work for you.
Thanks