"Username already taken" double tap - ios

When using the sign up flow in my app the alert view shows "Username already taken" when I double tap the signup button fast even though it's not taken. Is there a way to make sure that it want show the alert view when this happens or any other solution?
See my code below:
NSString *username = [self.usernameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *password = [self.passwordField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *email = [self.emailField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([username length] == 0 || [password length] == 0 || [email length] == 0) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Oops!"
message:#"Make sure you enter a username, password, and email address with at least 5 characters!"
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
else {
PFUser *newUser = [PFUser user];
newUser.username = username;
newUser.password = password;
newUser.email = email;
[newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Sorry!"
message:[error.userInfo objectForKey:#"error"]
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
else
{
[self saveUserDefaults];
[self dismissViewControllerAnimated:YES completion:nil];
[self performSegueWithIdentifier:#"setupProfile" sender:self];
}
}];
}
}

set
setUserInteractionEnabled:NO
to your button on the First tap.
Which wont allow any tap until you set it
setUserInteractionEnabled:YES

Related

i can't perform segue

Below is my code and I want to perform segue based on response string, but couldn't, can someone watch out my code and tell me what's wrong.
and when I am entering value in contact no. textfield,app is terminating with the error :
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't do regex matching on object <UITextField: 0x7faae2079980; frame = (20 353; 280 45); text = '9876543210'; clipsToBounds = YES; opaque = NO; autoresize = W+TM+BM; gestureRecognizers = <NSArray: 0x7faae2097250>; layer = <CALayer: 0x7faae2083bd0>>.'
Code I have try:
- (IBAction)Register:(id)sender {
if ((uname.text.length==0)&&(uemail.text.length==0)&&(ucontact.text.length==0)&&(upwd.text.length==0)&&(confirmpwd.text.length==0)){
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Can not Register"
message:#"All the Fields are Mandatory"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:#"Cancel", nil];
[alert show];
}
else{
NSString *emailString = uemail.text;// storing the entered email in a string.
//Regular expression to checl the email format.
NSString *emailReg = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest=[NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailReg];
if(([emailTest evaluateWithObject:emailString]!=YES)||[emailString isEqualToString:#""])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning" message:#"Enter your email in abc#example.com format." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
uemail.text = #"";
return;
}
}
NSString *mobile=ucontact.text;
NSString *numberRegEx = #"([0-9]{10})";
NSPredicate *numberTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", numberRegEx];
if ([numberTest evaluateWithObject:ucontact] != YES||[mobile isEqualToString:#""])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning" message:#"Enter a Valid Number." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
uemail.text = #"";
return;
}
if ((upwd.text)!=(confirmpwd.text)) {
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Password Do Not Match"
message:nil
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:#"Cancel", nil];
[alert show];}
NSURL *url = [NSURL URLWithString:#"http://108.179.246.128/connectme/service/registration.php"];
NSMutableURLRequest *rq = [NSMutableURLRequest requestWithURL:url];
[rq setHTTPMethod:#"POST"];
NSString *params=[NSString stringWithFormat:#"name=%#&email=%#&mobile=%#&passwrd=%#",self->uname.text,self->uemail.text,self->ucontact.text,self->upwd.text];
NSData *postData = [params dataUsingEncoding:NSASCIIStringEncoding];
[rq setHTTPBody:postData];
NSURLResponse *response;
NSError *error;
NSData *urlData = [NSURLConnection sendSynchronousRequest:rq returningResponse:&response error:&error];
NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; NSLog(#"Login response:%#",str); //getting response
// [self performSegueWithIdentifier:#"activity" sender:self];
if([str isEqual: #"Successfully Registered"])
{
[self performSegueWithIdentifier:#"login" sender:self];
}
else if([str isEqual:#"Sorry Email address already taken"])
{
UIAlertView *alertit=[[UIAlertView alloc] initWithTitle:#"Can not Register"
message:#"Email Address already Taken"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:#"Cancel", nil];
[alertit show];
}
}
For your second question that is crashing, it is because you need to pass the textField.text not the UITextField object. So it should be mobile variable that contain text of textField instead of ucontact textField object.
[numberTest evaluateWithObject:mobile]

How to set validation in UITextFields

I am using some text fields, and text field validations. I have shown below my code, In this code click button event and all text field text save on web server, In this code validation running only on empty text field. But i want to correct email address validation and all text field character length fixed.I tried to many times but some time condition wrong and some time don't show alert view and some time don't save text field text. How it possible please help, Thank you
My code
- (IBAction)submit:(id)sender {
if(self.txname == nil || [self.txname.text isEqualToString:#""])
{
UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:#"Name"message:#"All Fields are mandatory." delegate:nil cancelButtonTitle:#"OK"otherButtonTitles:nil, nil];
[ErrorAlert show];
}
else if(self.txemail == nil || [self.txemail.text isEqualToString:#""])
{
UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:#"Email"message:#"All Fields are mandatory." delegate:nil cancelButtonTitle:#"OK"otherButtonTitles:nil, nil];
[ErrorAlert show];
}
else if(self.tx_phone == nil || [self.tx_phone.text isEqualToString:#""])
{
UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:#"Phone"message:#"All Fields are mandatory." delegate:nil cancelButtonTitle:#"OK"otherButtonTitles:nil, nil];
[ErrorAlert show];
}
else if(self.txcomment == nil || [self.txcomment.text isEqualToString:#""])
{
UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:#"Comment"message:#"All Fields are mandatory." delegate:nil cancelButtonTitle:#"OK"otherButtonTitles:nil, nil];
[ErrorAlert show];
}
else
{
//Here YOUR URL
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:#"MY URL"]];
//create the Method "GET" or "POST"
[request setHTTPMethod:#"POST"];
//Pass The String to server(YOU SHOULD GIVE YOUR PARAMETERS INSTEAD OF MY PARAMETERS)
NSString *userUpdate =[NSString stringWithFormat:#"name=%#&email=%#&phone=%#& comment=%#&",_txname.text,_txemail.text,_tx_phone.text,_txcomment.text,nil];
//Check The Value what we passed
NSLog(#"the data Details is =%#", userUpdate);
//Convert the String to Data
NSData *data1 = [userUpdate dataUsingEncoding:NSUTF8StringEncoding];
//Apply the data to the body
[request setHTTPBody:data1];
//Create the response and Error
NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];
//This is for Response
NSLog(#"got response==%#", resSrt);
if(resSrt)
{
NSLog(#"got response");
}
else
{
NSLog(#"faield to connect");
}
{
UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:#"Success"message:#"All Fields are mandatory." delegate:nil cancelButtonTitle:#"OK"otherButtonTitles:nil, nil];
[ErrorAlert show];
}
[self.view endEditing:YES];
self.txname.text=#"";
self.txemail.text=#"";
self.tx_phone.text=#"";
self.txcomment.text=#"";
}
}
do like
- (IBAction)submit:(id)sender {
if (![txname hasText]) {
[self showAlertView:#"Alert" message:#"name is empty"];
}
else if (![txemail hasText])
{
[self showAlertView:#"Alert" message:#"email is empty"];
}
else if ([self isValidEmailAddress:txemail.text] == NO)
{
[self showAlertView:#"Alert" message:#"Invaildemail"];
}
else
{
// call webservice for succes
}
Create the alertcontroller
- (void)showAlertView:(NSString*)title message:(NSString*)message
{
UIAlertController* alertMessage = [UIAlertController
alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction* action){
}];
[alertMessage addAction:yesButton];
[self presentViewController:alertMessage animated:YES completion:nil];
}
for email validation
- (BOOL)isValidEmailAddress:(NSString *)emailAddress
{
//Create a regex string
NSString *stricterFilterString = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}" ;
//Create predicate with format matching your regex string
NSPredicate *emailTest = [NSPredicatepredicateWithFormat:
#"SELF MATCHES %#", stricterFilterString];
//return true if email address is valid
return [emailTest evaluateWithObject:emailAddress];
}
updated
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField == self.txname)
{
// Prevent crashing undo bug – see note below.
if(range.length + range.location > textField.text.length)
{
return NO;
}
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return newLength <= 25;
}
return YES;
}
I did it in my code, Do like following Way:
- (IBAction)submit:(id)sender {
if (![self isFormValid]) {
return;
}
NSError *error;
if (!error)
{
UIAlertView *signupalert = [[UIAlertView alloc]initWithTitle:#"Congratulations" message:#"Record Added Successfully" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[signupalert show];
}
}
-(BOOL)isFormValid
{
NSString *emailRegEx =#"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest =[NSPredicate predicateWithFormat:#"SELF MATCHES %#",emailRegEx];
if (txname.text && txname.text.length==0)
{
[self showErrorMessage:#"Please enter name"];
return NO;
}
else if (tx_phone.text && tx_phone.text.length!=10)
{
[self showErrorMessage:#"Please enter valid phone number"];
return NO;
}
else if([emailTest evaluateWithObject: txemail.text]==NO)
{
[self showErrorMessage:#"Please enter Valid Email_id"];
return NO;
}
else if (txcomment.text && txcomment.text.length==0)
{
[self showErrorMessage:#"Please enter comment"];
return NO;
}
return YES;
}
-(void)showErrorMessage:(NSString *)message
{
UIAlertView *alertmessage = [[UIAlertView alloc]initWithTitle:#"Error" message:message delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alertmessage show];
}
-(BOOL) NSStringIsValidEmail:(NSString *)checkEmail
{
BOOL stricterFilter = NO;
NSString *filter = #"^[A-Z0-9a-z\\._%+-]+#([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}$";
NSString *lstring = #"^.+#([A-Za-z0-9-]+\\.)+[A-Za-z]{2}[A-Za-z]*$";
NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegex];
return [emailTest evaluateWithObject:checkEmail];
}

ios- Login and Signup Button Wont Work (treehouse and parse)

My login and signup buttons WERE working but for some reason after I fill out the unsername and password field - and press the login button - the botton wont push to my inbox controller. But if i stop my project and then re run it, it will load as logged in. I don't know why! Any help would be appreciated--
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationItem.hidesBackButton = YES;
}
- (IBAction)login:(id)sender {
NSString *username = [self.usernameField.text
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *password = [self.passwordField.text
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([username length] == 0 || [password length] == 0) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Oops!"
message:#"Make sure you enter a username and password!"
delegate:Nil cancelButtonTitle:#"Okey"
otherButtonTitles:nil];
[alertView show];
} else {
[PFUser logInWithUsernameInBackground:username password:password block:^(PFUser *user, NSError *error) {
if (error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Sorry!"
message:[error.userInfo objectForKey:#"error"]
delegate:Nil cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alertView show];
} else {
[self.navigationController popToRootViewControllerAnimated:YES];
}
}];
}
}
#end

Facebook setting for Invite friend in Facebook

It may be an easy question but can anybody tell me.
Is there any setting we need to do in the app (in Facebook developer site) for invite friends?
I have done all in code to invite friends.
When I substitute the Facebook APP ID of a sample code in which invite friend is working, then my code is also working fine.
But when I substitute my APP ID then the process is going fine, showing me successfully invitation message, but invitation is not sent.
I am using the following code:
[FBWebDialogs presentRequestsDialogModallyWithSession:nil
message:FacebookInviteMessage
title:#"Invite Friends"
parameters:parameters
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
NSString *strResultUrl = [resultURL absoluteString];
if (result == FBWebDialogResultDialogCompleted && ![strResultUrl isEqualToString:#"fbconnect://success"])
{
MIDLog(#"Web dialog complete: %#", resultURL);
if (![resultURL query])
{
return;
}
NSDictionary *params = [self parseURLParams:[resultURL query]];
NSMutableArray *recipientIDs = [[NSMutableArray alloc] init];
for (NSString *paramKey in params)
{
if ([paramKey hasPrefix:#"to["])
{
[recipientIDs addObject:[params objectForKey:paramKey]];
}
}
if ([params objectForKey:#"request"])
{
NSLog(#"Request ID: %#", [params objectForKey:#"request"]);
}
if ([recipientIDs count] > 0)
{
//[self showMessage:#"Sent request successfully."];
//NSLog(#"Recipient ID(s): %#", recipientIDs);
UIAlertView *alrt = [[UIAlertView alloc] initWithTitle:#"Success!" message:#"Invitation(s) sent successfuly!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alrt show];
alrt = nil;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Success" message:FacebookInviteSuccess delegate:self cancelButtonTitle:#"Proceed" otherButtonTitles:nil];
alert.tag = 14114;
[alert show];
alert = nil;
}
else if (result == FBWebDialogResultDialogCompleted && [strResultUrl isEqualToString:#"fbconnect://success"])
{
MIDLog(#"Cancel Clicked");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Cancelled" message:nil delegate:self cancelButtonTitle:#"Proceed" otherButtonTitles:nil];
alert.tag = 14114;
[alert show];
alert = nil;
}
else {
MIDLog(#"Web dialog not complete, error: %#", error.description);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:InternetFailError delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.tag = 15115;
[alert show];
alert = nil;
}
}
friendCache:self.friendCache]
I provided the canvas URL in the application settings under Facebook developer site and It works for me

JSON login in iPhone

I am tring to perform Login through JSON ,using PHP webservices , but problem is that all time its give output unvalid user what can i do??
lementation LoginViewController
NSString *username;
NSString *password;
#synthesize arr,json;
- (IBAction)btnLogin:(id)sender {
username=txtUserName.text;
password=txtPassword.text;
NSString *post = [NSString stringWithFormat:#"user_name=%#&user_password=%#",username,password];
NSLog(#"post data value is:%#",post);
NSString *hostStr =#"http://myjsonwork.com/working_apps/?";
hostStr = [hostStr stringByAppendingString:post];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:hostStr]];
json=[NSJSONSerialization JSONObjectWithData:dataURL options:kNilOptions error:nil];
NSString *str=[json objectForKey:#"success"];
NSLog(#"post data value is:%#",hostStr);
NSLog(#"post dataURL value is:%#",dataURL);
if ([str isEqualToString:#"True"])
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Congrats" message:#"You are //authorised" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil,nil];
[alert show];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Error" message:#"Invalid Username and password" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil,nil];
[alert show];
}
}

Resources