UIAlertView, once the user opens the app. Shown once - iphone

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.

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

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.

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.

Related

fire an alert when a UILocalNotification is called

I have a UILocalNotification and I want to fire an alert when the app recovers from background to foreground. I can create the alert in didReceiveLocalNotification, but this method can only be called when the app is active. Now I want to check if a notification was fired while the app is in background and then fire an alert when the app recovers.
this is my current method
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
application.applicationIconBadgeNumber = 0;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSLog(#"prefs %#",[prefs stringForKey:#"kTimerNotificationUserDef"]);
if([prefs stringForKey:#"kTimerNotificationUserDef"] != nil)
{
[prefs setObject:nil forKey:#"kTimerNotificationUserDef"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Timer Alert" message:[prefs stringForKey:#"kTimerNotificationUserDef"] delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
the NSUserDefaults is set when I initialize the notification, however, this alert is called even though the notification did not arrive yet. So, Im assuming maybe I can do something like:
if([prefs stringForKey:#"kTimerNotificationUserDef"] != nil && didFireNotifFromBackground)
any suggestions? thanks!
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
notification.applicationIconBadgeNumber = 0;
NSString *reminderText = [notification.userInfo objectForKey:kRemindMeNotificationDataKey];
[viewController showReminder:reminderText];
}
show the alertview where class u want to show
- (void)showReminder:(NSString *)text
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Reminder" message:text delegate:nil cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
It is in your settings. Settings ->Notification->Your application in right side-> Choose your alert style.
Then you get alert directly while firing local notification

Showing an UIAlertView the first time the app is launched only

Am I doing something wrong in getting an UIAlertView to play the first time only? In my didFinishLaunchingWithOptions, my MainViewController gets instantiated. So in MainViewController's viewDidLoad, I do this:
BOOL shouldAlert = [[NSUserDefaults standardUserDefaults] boolForKey:#"ShowAlert"];
if (!shouldAlert) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"MyAlert" message:#"Some text here" delegate:self cancelButtonTitle:#"Continue" otherButtonTitles: nil];
[alert show];
[alert release];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"ShowAlert"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
When I try it on the device, it doesn't work if I double tap home, and hit the minus sign next to the app to remove it from the background mode (I think that's what double tapping does right?). Cause after I do that, the pop up appears again. If I don't do that, than the alert only shows once. Is this expected behavior? Thanks a bunch.
you get your shouldAlert first time from userdefauls?
I think you should check if the userdefaults exists else your bool is NO or FALSE
check this example:
NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
shouldAlert = YES; // define in the .h
if(userDef)
shouldAlert = [userDef boolForKey:#"ShowAlert"];
Try with an integer (0 / 1). I had some problems too with BOOL.
setInteger:forKey:
integerForKey:
EDIT
Try updating the defaults before initing/showing the alert.
And please, update your variable names. Your code is a nonsense with their current name.
Try this :
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
BOOL hasAlreadyBeenLaunched = [defaults boolForKey:#"HasAlreadyBeenLaunched"];
if (!hasAlreadyBeenLaunched) {
[defaults setBool:YES forKey:#"HasAlreadyBeenLaunched"];
[defaults synchronize];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"MyAlert" message:#"Some text here" delegate:self cancelButtonTitle:#"Continue" otherButtonTitles: nil];
[alert show];
[alert release];
}

Add Pop-up for database update

I'm a new iphone developer and getting ready to launch my first app. I have an reference utility app that I developed with help from an experienced developer to provide a process to upload database update to SQLite from access. When the updates occur, I have a popup window appearing which prompts the user to accept the data update or not. However, if they accept the update I want a popup to display during the update process. What do I need to do to ad this to this code?
if( [elementName isEqualToString:#"is_update_availableResult"] )
{
if([soapResults isEqualToString:#"yes"])
{
soapResults = [[NSMutableString alloc] init];
NSString *strMessage=#"An update is available. Select OK to update or cancel to load later. Please wait while system loads data before using app. ";
altView=[[UIAlertView alloc] init];
altView.title=#"MY APP";
altView.delegate=self;
[altView addButtonWithTitle:#"OK"];
[altView addButtonWithTitle:#"Cancel"];
[altView setCancelButtonIndex:0];
altView.message=strMessage;
[altView show];
}
Use the below code to implement the progress indication for your data base operation.
Declare UIAlertView* myAlert in your class header file.
myAlert = [[UIAlertView alloc] initWithTitle:#"Updating database…" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[myAlert show];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.center = CGPointMake(myAlert.bounds.size.width / 2, myAlert.bounds.size.height - 50);
[indicator startAnimating];
[myAlert addSubview:indicator];
[indicator release];
Once your database operation is completed dismiss the UIAlertView
-(void) OperationCompleted
{
[myAlert dismissWithClickedButtonIndex:0 animated:YES];
}

One time UIAlert?

I want to do a UIalert the first time a user opens my app, to tell them about basic functionality, but I don't want it showing up every time they open the app or reload the home screen. I've seen this in other apps but don't know what the best way of going about it would be. If anyone could point me in the right direction on this it would be appreciated.
Stick this into you App Delegate in the - (void)applicationDidFinishLaunching:(UIApplication )application method
BOOL hasRunBefore = [[NSUserDefaults standardUserDefaults] boolForKey:#"FirstRun"];
if (!hasRunBefore) {
UIAlertView *firstRun = [[UIAlertView alloc] initWithTitle:#"Virgin" message:#"Hello World!!" delegate:nil cancelButtonTitle:#"Done" otherButtonTitles:nil];
[firstRun show];
[firstRun release];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"FirstRun"];
}
else if (hasRunBefore) {
//can do some else if run before
}
Basically what this does is check for the key "FirstRun" in NSUserDefaults, similarly this could be modified to be a version number or something, say if you wanted a new alert after each update or something. It then checks if the key does not else (!hasRunBefore) and if it does not exist (so never been run before) it creates a UIAlertView and then sets YES for the key (also creating that ket in process)
Use "[NSUserDefaults standardUserDefaults]" to write a boolean if they've started the app before or not.
I use NSUserDefaults in the viewDidLoad method of my main screen. So every time the app starts and the main screen loads, it checks if its the user's first time.
This is how I do it in my app:
- (void)viewDidLoad {
BOOL tempBOOL = [[NSUserDefaults standardUserDefaults] boolForKey:#"hasSeenOpeningAlert"];
if (!tempBOOL) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Welcome To My App" message:#"This app will ... First you need to ..." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
[super viewDidLoad];
}
and then:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
Edit *editViewController = [[[Edit alloc] initWithNibName:#"Edit" bundle:nil]retain];
[self.navigationController presentModalViewController:editViewController animated:YES];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"hasSeenOpeningAlert"];
[[NSUserDefaults standardUserDefaults] synchronize];
[editViewController release];
}

How to get a UIAlertView's result?

I implemented the gamekit. All works fine now. But if the user presses on send the data will instantly send to the other iphone/ipod/ipad and it will instantly written.
So now i wanted to implemenr a confirm screen for the receiver.
In my receiveData method (from the gamekit) i have an array. If the user presses yes the array will be written into a file.if not it wont be written into a file.
#pragma mark -
#pragma mark - GKreceiveData
- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context
{
NSDictionary *dict = [NSKeyedUnarchiver unarchiveObjectWithData:data];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Incoming Set" message:[NSString stringWithFormat:#"%# wants to send you a Set named: \n\n %#",[session displayNameForPeer:peer], [dict valueForKey:#"SetName"]] delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 0)
{
//NSLog(#"ok");
//this should happen if the user presses on ok on the alertview.
[dataArray addObject:dict]; //i can't acess "dict"
}
else
{
//NSLog(#"cancel");
}
}
Do you see the problem?? What can I do??
dict is created as autorelease, so it will be deleted during UIAlertView show up.
Your CancelButton's index is == 0;
cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil
Seems, that your OK button index is 1.