How to handle username in Cocoa Touch? - iphone

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/

Related

Password Stored in the memory

During the login request, password get stored. After this request I'm setting the password string with an empty character.
Does it really store in memory, if it does how would I check? And how do I wipe that off.
I'm already using ARC in my App.
//Stroe user information
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:#"Moses" forKey:#"username"];
[prefs setInteger:42 forKey:#"age"];
[prefs synchronize];
//Getting the stored information
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *username = [prefs stringForKey:#"username"];
NSInteger age = [prefs integerForKey:#"age"];
Storing a password in NSUserDefaults is insecure, since anyone who get access to the iPhone, can view the password.
you can check the string is empty..via
if([yourString isEqualToString:#""]){
NSLog(#"empty string");
}

NSUserDefaults, saving data issue

So I have an app that lets you put in a title and an author on the front page, then you hit enter and it will go into the actual features of the app. I want to save the data with NSUserDefaults, so that when they click the enter button, it saves it *AND goes into the next view. I have it setup with the storybord already to go into the next view, but when I use this code:
-(IBAction)enter:(id)sender {
titleString = [[NSString alloc] initWithFormat:[title text]];
[title setText:titleString];
NSUserDefaults *titleDefault = [NSUserDefaults standardUserDefaults];
[titleDefault setObject:titleString forKey:#"stringkey"];
authorString = [[NSString alloc] initWithFormat:[author text]];
[title setText:authorString];
NSUserDefaults *authorDefault = [NSUserDefaults standardUserDefaults];
[authorDefault setObject:authorString forKey:#"stringkey2"];
}
It will always crash when you hit the button. I have all the NSStrings defined and such, so I don't see what the problem is. I also have it loading with:
title.text = [[NSUserDefaults standardUserDefaults] objectForKey:#"stringkey"];
author.text = [[NSUserDefaults standardUserDefaults] objectForKey:#"stringkey2"];
Under the viewdidload, so can I have some help as to why this wouldn't work?
Just to optimize your -enter:method:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:title.text forKey:#"stringkey"];
[defaults setObject:author.text forKey:#"stringkey2"];
[defaults synchronize]; // this will store them instantly!
For the crash: can you please provide us with the exact crash log? Maybe your Button is linked to an unknown selector...
First, there are a few questions about this: here, here. Try searching around first, and at least let people know why the duplicate questions you found didn't work for you.
Secondly, I think you might benefit from adding a synchronize. Use this:
titleString = [[NSString alloc] initWithFormat:[title text]];
[title setText:titleString];
NSUserDefaults *titleDefault = [NSUserDefaults standardUserDefaults];
[titleDefault setObject:titleString forKey:#"stringkey"];
[titleDefault synchronize];
authorString = [[NSString alloc] initWithFormat:[author text]];
[author setText:authorString];
NSUserDefaults *authorDefault = [NSUserDefaults standardUserDefaults];
[authorDefault setObject:authorString forKey:#"stringkey2"];
[authorDefault synchronize];
I also changed the [title setText:authorString]; line to [author setText:authorString];. I do, however, think it is a bit redundant the way you are allocating the NSStrings to the value of the text fields and then setting the text back to the value of the string you created. I don't get the need. Anyway, hope this helps.

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"];

How to keep my data in a array-iphone sdk

I'm trying to create an application, and in that I'm receiving some contents from net and loading into and array, if I quit and run the app again i'm losing the data. How can I store the data in the app itself. Should I use some kind of database? If so which one and how can I implement that? I need to store only some string variables.
EDIT:
My app is tabbar based app, and have two tabs. Loading the arrays in one tab and loading that array in a table in the other VC. once I load the array and move to the second tab, the table is loaded with the array. If i go back and add few more values and then if i check, the added datas is not displayed in the table. And also, when I quit the app the table lose all the values, I need to keep the values in the table, even after i quit the app. How can I do that?
here is my code:
in viewdidload:
NSUserDefaults *simplePlistDb = [NSUserDefaults standardUserDefaults];
[simplePlistDb setBool:YES forKey:#"isItWorking"];
[simplePlistDb setObject:alertList forKey:#"myArray"];
[simplePlistDb synchronize];
in cellforrowatindexpath:-
NSUserDefaults *simplePlistDb = [NSUserDefaults standardUserDefaults];
BOOL flag = [simplePlistDb boolForKey:#"isItWorking"];
if (flag)
{
NSArray *myArray = [simplePlistDb objectForKey:#"myArray"];
for (NSString *str in myArray){
NSLog(#"Str:%#", str);
[loadArray addObject:str];
}
cell.textLabel.text = [loadArray objectAtIndex:indexPath.row];
}
Thank you.
You will want to look into Core Data if you want to save a decent amount of data to the iPhone. However, if not, you can just load the items in the array into a plist and load them from there at launch.
Plist Writing: How to create a new custom property list in iPhone Applications
CoreData: http://www.raywenderlich.com/934/core-data-tutorial-getting-started
Edit--->
As Nekto said, you can also use NSUserDefaults, but I advise mainly using NSUserDefaults for simple NSStrings and integers.
I think for your purposes NSUserDefaults will be enough : NSUserDefaults Class Reference.
Examples:
// save
NSUserDefaults *simplePlistDb = [NSUserDefaults standardUserDefaults];
[simplePlistDb setBool:YES forKey:#"isItWorking"];
[simplePlistDb setObject:[NSArray arrayWithObjects:#"very", #"cool", nil]];
[simplePlistDb synchronize];
// restore
NSUserDefaults *simplePlistDb = [NSUserDefaults standardUserDefaults];
BOOL flag = [simplePlistDb boolForKey:#"isItWorking"];
if (flag)
{
NSArray *myArray = [simplePlistDb objectForKey:#"myArray"];
for (NSString *str in myArray)
NSLog(#"%#", str);
}
You can use NSUserDefaults in order to store your data till the time your app is installed in your device.
How to write in NSuserDefaults:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:#"yourObject1" forKey:#"correspopndingKey1"];
[defaults setObject:#"yourObject2" forKey:#"correspopndingKey2"];
[defaults synchronize];
How to read from NSuserDefaults:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *temp1 = [defaults objectForKey:#"correspopndingKey1"];
NSString *temp2 = [defaults objectForKey:#"correspopndingKey2"];
You can store NSArray in a similar way.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:objArray forKey:#"correspopndingKey"];
[defaults synchronize];

Security in iPhone

My problem is :
1. I am developing an iPhone application i have proceded like this :
the user authentication, with a user name and a password ( after a verification with web services), my question is how it is stored the username and the password in my application. are there secured or did i proced to implement a secure mechanism ?
thanks for your answer
I am using SFHFKeychainUtils. It uses apple's Security Framework.
It's easy to use, to save a Username and Password:
[SFHFKeychainUtils storeUsername:userNameString andPassword:passwordString forServiceName:#"YourServiceName" updateExisting:TRUE error:&error];
and to retrieve the encrypted password for a Username
[SFHFKeychainUtils getPasswordForUsername:userNameString andServiceName:#"YourServiceName" error:&error];
Use the Keychain : Keychain iphone
Saving:
NSUserDefaults *userNamePrefs = [NSUserDefaults standardUserDefaults];
NSUserDefaults *passwordPrefs = [NSUserDefaults standardUserDefaults];
[userNamePrefs setObject:#"username" forKey:#"username"];
[passwordPrefs setObject:#"password" forKey:#"password"];
Retrieving
NSUserDefaults *userNamePrefs = [NSUserDefaults standardUserDefaults];
NSUserDefaults *passwordPrefs = [NSUserDefaults standardUserDefaults];
NSString *savedUsername = [prefs stringForKey:#"username"];
NSString *savedPassword = [prefs stringForKey:#"username"];
what else UITextbox User enter just match the string like
if ((usernameTextBox.Text isEqualToString(savedUsername)) && (passwordTextBox.Text isEqualToString(savedPassword)) ){
// pass authentication
}
else{
// show alert view fail authetication
}