cache username and password in webview (iphone) - iphone

I want to introduce a cache policy in my app.
Basically I have made a web view in which I have the functionality that when I open up that I'll have to login ,
I want to store that data in the cache now ..
like Facebook app .. when I delete that app from the background and open it again .. i dont have to add all the detail again.
how to do this ?

Did you try NSUserDefaults
NSString *value = #"MyUsername#domain";
NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults];
[userPreferences setObject:value forKey:#"username"];
To fetch from UserDefaults -
NSString *_value= [[NSUserDefaults standardUserDefaults] stringForKey:#"username"];

Related

how to disable re-login in iphone

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.

How to handle username in Cocoa Touch?

I have a login view controller that uses the php/mysql/POST function to verify if a user is authorized. Once authorized, they enter the application.
My question is, once they are in the application, how do I "grab" their username and use it to append to any data that they upload to my database while they are in my app?
I am assuming I append it to my current NSMutableData string that carries the text blocks that they upload, but how would I "grab" this username to append it?
Assuming I have two view controllers, "login" and "main", and the content is being uploaded on "main", how do I pass the username from the "login" view to the "main" view so that I can append it when the user hits the upload button (which resides on the main view)?
EDIT: Where I want this username retrieved is in the following:
-(IBAction)uploadImage:(id)sender {
NSLog(#"uploadImage");
//and for retrieval
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *authenticatedUsersName = [defaults objectForKey:#"username"];
NSMutableString *postString = [NSMutableString stringWithString:kPostURL];
[postString appendString:[NSString stringWithFormat:#"&%#=%#", kText, textField.text]];
[postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
If you're not concerned with security of a username, why don't you store the username in NSUserDefaults upon successful authentication? You can do something like this....
//upon authentication...
NSUserDefault *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"username"] == nil) //we haven't saved anything yet
[defaults setObject:authenticatedUsersName forKey:#"username"];
[defaults synchronize]; //saves the new defaults
//and for retrieval
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *authenticatedUsersName = [defaults objectForKey:#"username"];
I think this design gives you more flexibility so you can use the username across all view controllers in your app.
post re-edit
Why can't you just format the string somewhere in this function? Whats the exact post url format?
-(IBAction)uploadImage:(id)sender {
NSLog(#"uploadImage");
//and for retrieval
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *authenticatedUsersName = [defaults objectForKey:#"username"];
//MY CHANGES, CAN YOU DO EITHER OF THESE?
NSMutableString *postString = [NSMutableString stringWithString:[NSString stringWithFormat:#"%#-%#", kPostURL, authenticatedUsersName];
[postString appendString:[NSString stringWithFormat:#"&%#=%#&%#", kText, textField.text, authenticatedUsersName]];
[postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
}
An alternative method of having globally available variables is to use a singleton class.
This is slightly more difficult than Convolution's solution using NSUserDefaults, but it suits some use cases better.
There is a good tutorial at the following location:
http://www.galloway.me.uk/tutorials/singleton-classes/

Iphone App Registration User Data Log

At the start of my application i make my users to register first then take them to login View.I want to save the successfully registered users and let them get to the loginView instead of Register View.
How can i do this.As far i know iphone is registered as one user.Tell me what is the best way.And how to keep the user data and redirect to login view for a user?
Thanks
You can use NSUserDefaults to save user data such as login info.
Take a look at this tutorial and the reference
In Short, you would do something like:
Saving after registration:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:#"UserName" forKey:#"UserName"];
[prefs setObject:#"Pass" forKey:#"Pass"];
// This is suggested to synch prefs, but is not needed
[prefs synchronize];
Retrieving on app start:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *userName = [prefs stringForKey:#"UserName"];
NSString *pass = [prefs stringForKey:#"Pass"];

Ios/iphone create/maintain websessions

How to do I create a web/server session by accessing the web server from ios / iphone.
I have a app where I ask user for a username and password and using that information I want to login to a website using UIWebView and access the necessary information. Greatly aprreciate if you can answer my question.
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *userType = [[loginArray valueForKey:#"Home"] valueForKey:#"userlogin_type"];
NSString *userLoginID = [[loginArray valueForKey:#"Home"] valueForKey:#"userlogin_id"];
NSString *userPwd = [[loginArray valueForKey:#"Home"] valueForKey:#"userlogin_password"];
//[prefs setObject:userPwd forKey:#"new_user_pwd"];
[prefs setObject:userType forKey:#"userType"];
[prefs setObject:userLoginID forKey:#"userLoginID"];
[prefs synchronize];//important this line
by using this u can maintain the session by verify this objects.
if u use the facebook sdk then
use
if(facbookObj.isSessionValid){
//session valid
}

XCode Cache Tutorial

Hey, I am trying to cache info on my IPhone App, but don't even know where to get started, I would to just cache a string (ex: 123123-12313123-1231231-123123), so when the user comes back it will remember it.
Is there a tutorial I can follow some where or could someone provide me some code?
Thanks
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setString:#"123123123" forKey:#"myKey"];
[userDefaults synchronize];
Later, when you wish to retrieve this object:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *myString = [userDefaults stringForKey:#"myKey"];
You'll, of course, want to manage the keys properly, etc.