laaaa
laaaa

Reputation: 119

UIAlertView, once the user opens the app. Shown once

I want a UIAlertView to show, once the user opens the app. It'll ask them for their email address. But i want it to only show once. So when the user re opens the app the uialertview shouldnt pop up. And the UIAlertView will contain 2 buttons within that. 'Dismiss' & 'Yes' .. the dismiss button will continue with the app. But the 'Yes' will take them to another view'.

Thanks :) Edit:

- (void)viewDidLoad {
    [super viewDidLoad];

if (![@"1" isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:@"alert"]]) {
    [[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:@"alert"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:@"Enter your email" 
                                                     message:@"\n\n\n" 
                                                    delegate:nil 
                                           cancelButtonTitle:@"Cancel" 
                                           otherButtonTitles:@"Enter", nil];

    textField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 50.0, 260.0, 25.0)]; 
    [textField setBackgroundColor:[UIColor whiteColor]];
    [textField setPlaceholder:@"enter email here"];
    [prompt addSubview:textField];



    [prompt show];
    [prompt release];


    //[textField becomeFirstResponder];

}
}

Okay this is the code at the moment, im stuck on how the email is going to be sent to be once the user presses enter.

Upvotes: 0

Views: 323

Answers (3)

Patrick Burleson
Patrick Burleson

Reputation: 388

First, need to make yourself the delegate for the UIAlertView so you know when the user has pressed Cancel or Enter. So this:

UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:@"Enter your email" 
                                                 message:@"\n\n\n" 
                                                delegate:self
                                       cancelButtonTitle:@"Cancel" 
                                       otherButtonTitles:@"Enter", nil];

The delegate callback is this method:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

Once that's called, you can retrieve the email address from the textField instance you created and added to the alert view.

Upvotes: 1

sergio
sergio

Reputation: 69027

You could store the email address in NSUserDefaults (I guess you are already doing it, if you ask for it only the very first time your app starts); each time the app starts, you check if the email address is there (NSUserDefaults); if not, you display the UIAlertView.

Upvotes: 1

Sam
Sam

Reputation: 3659

You can use NSUserDefaults to save a boolean between sessions.

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html

// save 
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:true] forKey:@"hasRunBefore"];

// load 
[[NSUserDefaults standardUserDefaults] objectForKey:@"hasRunBefore"] boolValue];

Upvotes: 2

Related Questions