How can I make UIAlertView modal? - ipad

I want to show a login dialogue and login error dialogue if necessary.
I use UIAlertView to show these dialogues, but the process keep running while showing the UIAlertView.
I wrote a code below. Now NSUserDefaults doesn't keep those value, so I expected login dialogue is shown and wait until button to be tapped.
But when run this, error dialogue is shown and after tapping OK for this, login dialogue is shown.
How can I fix this?
Thanks in advance.
- (void)storeEvernote
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
evID = [defaults stringForKey:#"evID"];
evPW = [defaults stringForKey:#"evPW"];
NSLog(#"%#", evID);
if( evID == NULL || evPW == NULL )
{
login = true;
[self showLoginDialogue];
}
else
{
evernoteID = evID;
evernotePW = evPW;
}
if( evernoteID == NULL || evernotePW == NULL )
{
login = false;
[self showErrMessage];
return;
}
[self getEvernoteNotebooks];
}
- (void)showLoginDialogue
{
UIAlertView *loginDialogue = [[UIAlertView alloc] initWithTitle:#"Evernote Login" message:#"Enter your Evernote Info" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:#"Login", nil];
loginDialogue.delegate = self;
loginDialogue.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[loginDialogue show];
}
- (void)showErrMessage
{
UIAlertView *loginalert = [[UIAlertView alloc] initWithTitle:#"Login Failure" message:#"Invalid ID & Password" delegate:nil cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[loginalert show];
}

The first time that storeEvernote method runs, evID and evPW are NULL so the first if is true, it shows the LoginDialoge then it continues to second if and because evernoteID and evernotePW are still NULL so the condition in second if statement is also true, so it shows the errorMessage. To fix this, move the second if statement to delegate method of the loginDialogue ALSO ADD return; to the end of first if statement for example:
- (void)storeEvernote
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
evID = [defaults stringForKey:#"evID"];
evPW = [defaults stringForKey:#"evPW"];
NSLog(#"%#", evID);
if( evID == NULL || evPW == NULL )
{
login = true;
[self showLoginDialogue];
return;
}
else
{
evernoteID = evID;
evernotePW = evPW;
}
[self getEvernoteNotebooks];
}
//The login dialoge delegate method:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//Process user input
...
if( evernoteID == NULL || evernotePW == NULL )
{
login = false;
[self showErrMessage];
return;
}
[self getEvernoteNotebooks];
}

Related

UIAlertView not calling

I'm attempting to create a simple webpage browser-esque application, where whenever the user wants to use a back, home, or forward function they are asked through an alert view to confirm.
The console window confirms that it has no problem calling the IBAction but comes up blank whenever I expect it to call Alert View.
Any help would be appreciated.
- (IBAction)controlPanelActions:(id)sender{
if (_controlPanel.selectedSegmentIndex == 0)
{
if ([_viewWeb canGoBack])
{
[self askToGoHome:nil];
}
}
- (IBAction)askToGoHome:(id)sender {
UIAlertView *alertDialog;
alertDialog.tag = 2;
alertDialog = [[UIAlertView alloc]
initWithTitle: #"Confirm"
message:#"Continue with Home Action?"
delegate: self
cancelButtonTitle: #"Yes"
otherButtonTitles: #"No", nil];
[alertDialog show];
}
- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if (alertView.tag == 1)
{
if ([buttonTitle isEqualToString:#"Yes"])
{
NSLog(#"back - yes");
[_viewWeb goBack];
}
}
if (alertView.tag == 2)
{
NSLog(#"home - pre-yes");
if ([buttonTitle isEqualToString:#"Yes"])
{
NSLog(#"home - yes");
HOMEPAGEURL = [[NSURL alloc] initWithString:HOMEPAGE];
[self.viewWeb loadRequest:[NSURLRequest requestWithURL:HOMEPAGEURL]];
}
}
if (alertView.tag == 3)
{
if ([buttonTitle isEqualToString:#"Yes"])
{
[_viewWeb goForward];
}
}
}
You set alertDialog.tag = 2;before you call init.
So,everytime you set tag,you set tag to a nil.It will not work.
Alertview alloc and init method sets its tag to 0.
So alertDialog.tag = 2;
will not work.
add this line after alloc and init method.

How to set a selector method to a textfield for textchange in objective C?

I'm writing a signup view controller for my app. I needed to validate the form. I got the idea that setting a selector method for text value change should work for different textfields containing the form data.
I saw old questions and stuff on google and this is what I have so far
- (void)viewDidLoad
{
[super viewDidLoad];
self.passwordInput.secureTextEntry = YES;
self.btnDone.enabled = NO; //Done button needs to be disabled until form is properly validated
self.emailInput.delegate = self; //emailinput is the property attached to Email textfield of the form
self.passwordInput.delegate = self;
emailCheck = NO;
passwordCheck = NO;
[self.emailInput addTarget:self action:#selector(formValidation) forControlEvents:UIControlEventValueChanged];
[self.passwordInput addTarget:self action:#selector(formValidation) forControlEvents:UIControlEventValueChanged];
// Do any additional setup after loading the view from its nib.
}
-(void) formValidation {
NSString *regex = #"[^#]+#[A-Za-z0-9.-]+\\.[A-Za-z]+";
NSPredicate *emailPredicate = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", regex];
if(self.passwordInput.text.length >7)
{
passwordCheck = YES;
}
if([emailPredicate evaluateWithObject:self.emailInput.text])
{
emailCheck = YES;
}
if (self.passwordInput.text.length<7 || ![emailPredicate evaluateWithObject:self.emailInput])
{
self.warningLabel.text = #"Please enter a valid email/at least 8 character password";
}
if(passwordCheck == YES && emailCheck ==YES)
{
self.btnDone.enabled = YES;//button is enabled
} }
Now the problem is that the event is not firing off. Nothing happens when enter the data. Can anyone guide me what I'm doing wrong here?
P.s. i don't quite understand UITextFieldTextDidChangeNotification. If someone can suggest an alternative solution or explain that concept for me, it'd be awesome
I just tried forControlEvents:UIControlEventEditingChanged, the app crashes with error that it can't perform regular expression.**"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't do regex matching on object <UITextField: 0x9ae8610;"**
Set the delegate of textfields and use the following method
-(void)textFieldDidEndEditing:(UITextField *)textField
{
[self validateForm:textField.text];
}
and change form validator function to
-(void) validateForm:(NSString *)inputString {
//validate inputString
}
on button submit click of form even you can check validation like below.
- (IBAction)pushToSignConfirmationScreen:(id)sender;
{
NSString *emailString = txt_Email.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 ([txt_ContactName.text length] == 0 || [txt_Address.text length] == 0
|| [txt_DBAName.text length] == 0 || [txt_City.text length] == 0
|| [txt_Email.text length] == 0 || [txt_Phone.text length] == 0
|| [txt_State.text length] == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning" message:#"All fields are required to begin setting up a merchant account." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
return;
}
else 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];
txt_Email.text = #"";
[btn_next setImage:[UIImage imageNamed:#"tag_icon_bt_up1.png"] forState:UIControlStateNormal];
return;
}
else if([txt_ZipCode.text length]!=5)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Warning" message:#"Please enter a valid zip code." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
txt_ZipCode.text = #"";
return;
}
else if([txt_Phone.text length]!=10 )
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Warning" message:#"Please enter a valid mobile number." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
txt_Phone.text = #"";
return;
}
First set the delegate for textfield, then do check in Method:-
- (BOOL)textFieldShouldReturn:(UITextField *)textField.
If textField is same then call your formValidation method.

Navigation in iOS using Objective-C

i have an ios app which uses a login page and after authenticating it enters into the inbox page.But after entering the inbox page it comes back automatically to the login page
Login.m
{
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:#"OK" otherButtonTitles:nil];
[alertView show];
}
else
{
NSString *query = [NSString stringWithFormat:#"SELECT * FROM Login_Info WHERE username='%#'",username]; // Execute the query.
NSLog(#" query = %#", query );
// Get the results.
if (self.arrLogin_Info != nil) {
self.arrLogin_Info = nil;
}
self.arrLogin_Info = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:query]];
[def setObject:[self.arrLogin_Info objectAtIndex:0] forKey:#"idKey"];
[def setObject:[self.arrLogin_Info objectAtIndex:1] forKey:#"usernameKey"];
[def setObject:[self.arrLogin_Info objectAtIndex:2] forKey:#"passwordKey"];
[def setObject:[self.arrLogin_Info objectAtIndex:3] forKey:#"emailKey"];
NSLog(#" query output = %#", self.arrLogin_Info);
NSString *val = [self.arrLogin_Info objectAtIndex:2];
// NSLog(#" val = %#",val);
if ([val isEqualToString:password] )
{
// NSLog(#" Inside if before entering app");
[self.navigationController popToRootViewControllerAnimated:YES];
}
else
{
//NSLog(#" Inside else before entering app");
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Sorry!"
message:#"Please ensure you have entered the correct password!"
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
}
}
#end
Inbox.m
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
id u = [def objectForKey:#"idkey"];
if(u)
{
NSString *query = [NSString stringWithFormat:#"Select *from Messages where recipient_ID=%#",u];
self.msg = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:query]];
// [self.tableView reloadData];
}
else
{
[self performSegueWithIdentifier:#"showLogin" sender:self];
}
// [self.tableView reloadData];
}
- (IBAction)logout:(id)sender {
//[PFUser logOut];
[self performSegueWithIdentifier:#"showLogin" sender:self];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"showLogin" ])
{
[segue.destinationViewController setHidesBottomBarWhenPushed:YES];
}
}
Your Inbox view controller uses the presence of an object for the key 'idkey' in NSUserDefaults to determine whether the user is already logged in, or whether to show the login screen.
I presume that this line in login.m
[def setObject:[self.arrLogin_Info objectAtIndex:0] forKey:#"idKey"];
is supposed to be setting that key, but you don't show where you initialise def - so my guess is that this is nil and you aren't saving the data in NSUserDefaults.
Also, all of this -
if (self.arrLogin_Info != nil) {
self.arrLogin_Info = nil;
}
self.arrLogin_Info = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:query]];
can be simplified to this
self.arrLogin_Info = [self.dbManager loadDataFromDB:query];

iOS - UIAlertView outton - otherbutton not working as it should

I'm having trouble with my app. Problem is when I press the Cancel button on the AlertView. It doesn't show the "Cancel" text that should be appearing at my output. The Confirm and Show Password buttons are working fine, both show the NSLogs, only the cancel buttons don't. Here is my code. Please be patient with me because I'm new in Xcode.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Confirm"])
{
UITextField *password = [alertView textFieldAtIndex:0];
NSLog(#"Password: %#", password.text);
if (buttonIndex != [alertView cancelButtonIndex])
{
NSLog(#"cancel");
}
else
{
NSLog(#"confirm");
entries = [[NSMutableArray alloc]init];
NSString *select = [NSString stringWithFormat:#"SELECT * FROM summary2 WHERE username = '%s' and pass = '%s'",[self.lbUser.text UTF8String],[password.text UTF8String]];
sqlite3_stmt *statement;
if (sqlite3_prepare(user, [select UTF8String], -1, &statement, nil)==SQLITE_OK)
{
if(sqlite3_step(statement)==SQLITE_ROW)
{
NSLog(#"database updated");
[self updatedatabase];
UIAlertView *alert3 = [[UIAlertView alloc]initWithTitle:#"Done" message:#"Account was updated successfully!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert3 show];
}
else
{
NSLog(#"Authentication Failed!");
UIAlertView *alert2 = [[UIAlertView alloc]initWithTitle:#"Failed" message:#"Wrong Password! Account was not updated." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert2 show];
NSLog(#"fail");
}
}
}
}
else if([title isEqualToString:#"View Password"])
{
UITextField *password = [alertView textFieldAtIndex:0];
NSLog(#"Password: %#", password.text);
if (buttonIndex != [alertView cancelButtonIndex])
{
NSLog(#"cancel");
}
else
{
NSLog(#"confirm");
entries = [[NSMutableArray alloc]init];
NSString *select = [NSString stringWithFormat:#"SELECT * FROM summary2 WHERE username = '%s' and pass = '%s'",[self.lbUser.text UTF8String],[password.text UTF8String]];
sqlite3_stmt *statement;
if (sqlite3_prepare(user, [select UTF8String], -1, &statement, nil)==SQLITE_OK)
{
if(sqlite3_step(statement)==SQLITE_ROW)
{
NSLog(#"database updated");
[self switchbtn];
}
else
{
//switch1.on=YES;
NSLog(#"Authentication Failed!");
UIAlertView *alert2 = [[UIAlertView alloc]initWithTitle:#"Failed" message:#"Wrong Password! Cannot view password." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert2 show];
NSLog(#"fail");
}
}
}
}
}
According to your if,if-else statements you're only looking for "View Password" and "Confirm" buttons. There is no branch in your if-statement to examine the cancel button.

FBConnect - sessionValid - AFTER delete application at the facebook-website

In my iOS iPhone app I use facebook connect. The SSO works wonderfully, I can post to wall etc., But there is one problem:
I start my app
allow facebook to connect
everything is fine in the app
BUT NOW ...
I delete the application on the facebook website!
If I start my app now - the NSLOG says, the FB-Connections is OK. ..?
So I thought, If I delete the applications online in my account, the iphone app has NO validSession and has to ask for a new login, but this failed.
What is wrong? How can I check that in the right way? Or how log is the sessionValid? Is there a timelimit before the method will run again? So was my check (delete online, start app again) to quick?
---- UPDATE:
- (void)fbLogin:(id)sender{
NSLog(#"FB Login Alert");
[self checkForPreviouslySavedAccessTokenInfo];
if (!isConnected == YES) {
NSLog(#"NO - Facebook Connection");
UIAlertView *popupFacebook = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(#"Headline_FacebookL", #"Headline")
message:NSLocalizedString(#"Facebook-Text", #"Facebook")
delegate:self
cancelButtonTitle:NSLocalizedString(#"later",#"Facebook Login später")
otherButtonTitles:NSLocalizedString(#"Facebook",#"Login"), nil];
popupFacebook.tag = alertFacebook;
[popupFacebook show];
[popupFacebook release];
}
else{
NSLog(#"Facebook Connection OK");
}
}
(void)checkForPreviouslySavedAccessTokenInfo:
-(void)checkForPreviouslySavedAccessTokenInfo{
// Initially set the isConnected value to NO.
isConnected = NO;
NSLog(#"FB Status erst mal auf NEIN");
// Check if there is a previous access token key in the user defaults file.
appDelegate.facebook = [[Facebook alloc] initWithAppId:#"XXXXXXXXX" andDelegate:(AppDelegate *) [[UIApplication sharedApplication] delegate]];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
appDelegate.facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
appDelegate.facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
// Check if the facebook session is valid.
// If it’s not valid clear any authorization and mark the status as not connected.
if (![appDelegate.facebook isSessionValid]) {
//[facebook authorize:permissions];
isConnected = NO;
NSLog(#"FB NO");
}
else {
isConnected = YES;
NSLog(#"FB YES");
}
}
SWITCH:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(#"clicking");
//NSString *title = [actionSheet buttonTitleAtIndex:buttonIndex];
if (alertView.tag == alertWelcome ) {
if (buttonIndex == 0) {
NSLog(#"close");
}
}
else if (alertView.tag == alertFacebook ) {
if (buttonIndex == 0) {
NSLog(#"später");
}
if (buttonIndex == 1) {
//self.label.text = NSLocalizedString(#"Facebook",#"Login"),
[self fbActive:nil];
NSLog(#"Login to FB");
}
}
}
Permissions:
-(void)fbActive:(id)sender{
if (![appDelegate.facebook isSessionValid]) {
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"user_likes",
#"read_stream",
#"user_status",
#"publish_stream",
//#"publish_actions",
//#"manage_pages",
#"read_requests",
nil];
[appDelegate.facebook authorize:permissions];
NSLog(#"FB - Permissions");
[permissions release];
}
}

Resources