Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How do you show an UIAlert only once for the life of the app?

kryekrye Posts: 362Registered Users
edited February 2011 in iOS SDK Development
I implemented an UIAlert in my app that pops up after the app has loaded. It basically gives you a "press the button on the bottom of your screen for more info" message. You press OK and it goes away. But you should only see that alert the very first time you launch the app. Every time after that you should not see it again.

How do you do that? How do you get a UIAlert to only display once for the life of the app?
Post edited by krye on
<a href="http://ineedcoffeetocode.com"; target="_blank">iNeedCoffeeToCode.com</a> <a href="http://rescuemyclassicmac.com"; target="_blank">RescueMyClassicMac.com</a> <a href="http://ryemac3.net"; target="_blank">RyeMAC3.net</a>

Replies

  • wtngclanwtngclan Posts: 102Registered Users
    edited August 2009
    krye wrote: »
    I implemented an UIAlert in my app that pops up after the app has loaded. It basically gives you a "press the button on the bottom of your screen for more info" message. You press OK and it goes away. But you should only see that alert the very first time you launch the app. Every time after that you should not see it again.

    How do you do that? How do you get a UIAlert to only display once for the life of the app?

    I would use NSUserDefaults. This is the gist of what I would do:

    1. Load key for 'alertshown' in NSUserDefaults
    2. If alertshown == nil then show the alert
    3. after alert is shown for the first time set 'alertshown' to anything you want

    Your alert will only be displayed once then.
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *alertshown = [defaults stringForKey:@"alertshown"];
    if (alertshown == nil) {
          //code for alert here
          [defaults setObject:@"alertwasdisplayed" forKey:@"alertshown"];
    }
    
    <b>Contact Info:</b><br />
    Phone: (347) 9-HUSTLE<br />
    E-Mail: <a href="mailto:ihustleapps@gmail.com">ihustleapps@gmail.com</a><br />
    Website: <a href="http://www.ihustleapps.com"; target="_blank">www.ihustleapps.com</a><br />
    100% Uncensored iPhone Ap
  • kryekrye Posts: 362Registered Users
    edited August 2009
    That worked like a charm. Thank you so so much.
    <a href="http://ineedcoffeetocode.com"; target="_blank">iNeedCoffeeToCode.com</a> <a href="http://rescuemyclassicmac.com"; target="_blank">RescueMyClassicMac.com</a> <a href="http://ryemac3.net"; target="_blank">RyeMAC3.net</a>
  • wtngclanwtngclan Posts: 102Registered Users
    edited August 2009
    krye wrote: »
    That worked like a charm. Thank you so so much.

    No problem. glad I could help.. I was looking over some of your past posts and I said you were trying to get a uitableview to load images from an array or sql database. You ever get that working? That's MY next project :)
    <b>Contact Info:</b><br />
    Phone: (347) 9-HUSTLE<br />
    E-Mail: <a href="mailto:ihustleapps@gmail.com">ihustleapps@gmail.com</a><br />
    Website: <a href="http://www.ihustleapps.com"; target="_blank">www.ihustleapps.com</a><br />
    100% Uncensored iPhone Ap
  • kryekrye Posts: 362Registered Users
    edited August 2009
    wtngclan wrote: »
    No problem. glad I could help.. I was looking over some of your past posts and I said you were trying to get a uitableview to load images from an array or sql database. You ever get that working? That's MY next project :)

    Yes I got that working. I used this tutorial as a base

    The trouble with it is that the images are on the web. I wanted to keep the images locally. You place your images in your resource folder and change this code to get it to work:

    CHANGE:
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[animal imageURL]]];
    UIImage *animalImage = [[UIImage alloc] initWithData:imageData cache:YES];
    self.animalView.animalImage.image = animalImage;
    

    TO:
    UIImage *animalImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[animal imageURL] ofType:@”jpg”]];
    
    self.animalView.animalImage.image = animalImage;
    
    <a href="http://ineedcoffeetocode.com"; target="_blank">iNeedCoffeeToCode.com</a> <a href="http://rescuemyclassicmac.com"; target="_blank">RescueMyClassicMac.com</a> <a href="http://ryemac3.net"; target="_blank">RyeMAC3.net</a>
  • wtngclanwtngclan Posts: 102Registered Users
    edited August 2009
    Would you be willing to email me your fruit project with it all implemented? :) pllllleeaase lol. My email is vmspot@gmail.com you could def save me some trouble tonight. Thanks
    krye wrote: »
    Yes I got that working. I used this tutorial as a base

    The trouble with it is that the images are on the web. I wanted to keep the images locally. You place your images in your resource folder and change this code to get it to work:

    CHANGE:
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[animal imageURL]]];
    UIImage *animalImage = [[UIImage alloc] initWithData:imageData cache:YES];
    self.animalView.animalImage.image = animalImage;
    

    TO:
    UIImage *animalImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[animal imageURL] ofType:@”jpg”]];
    
    self.animalView.animalImage.image = animalImage;
    
    <b>Contact Info:</b><br />
    Phone: (347) 9-HUSTLE<br />
    E-Mail: <a href="mailto:ihustleapps@gmail.com">ihustleapps@gmail.com</a><br />
    Website: <a href="http://www.ihustleapps.com"; target="_blank">www.ihustleapps.com</a><br />
    100% Uncensored iPhone Ap
  • kryekrye Posts: 362Registered Users
    edited August 2009
    wtngclan wrote: »
    Would you be willing to email me your fruit project with it all implemented? :) pllllleeaase lol. My email is vmspot@gmail.com you could def save me some trouble tonight. Thanks

    All you have to do is change the one method I showed you. My project isn't "fruit" anymore anyway, so the SQL database would do you no good.
    <a href="http://ineedcoffeetocode.com"; target="_blank">iNeedCoffeeToCode.com</a> <a href="http://rescuemyclassicmac.com"; target="_blank">RescueMyClassicMac.com</a> <a href="http://ryemac3.net"; target="_blank">RyeMAC3.net</a>
  • taylorsuktaylorsuk Posts: 11Registered Users
    edited February 2011
    krye wrote: »
    Yes I got that working. I used this tutorial as a base

    The trouble with it is that the images are on the web. I wanted to keep the images locally. You place your images in your resource folder and change this code to get it to work:

    CHANGE:
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[animal imageURL]]];
    UIImage *animalImage = [[UIImage alloc] initWithData:imageData cache:YES];
    self.animalView.animalImage.image = animalImage;
    

    TO:
    UIImage *animalImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[animal imageURL] ofType:@”jpg”]];
    
    self.animalView.animalImage.image = animalImage;
    

    when i use the local code that you sent i get a error. here is my code if you can help??
    [/code]
    // Set the title of the view to the animal's name
    self.animalView.title = [animal name];
    // Set the description field to the animals description
    [self.animalView.animalDesciption setText:[animal description]];
    // Load the animals image into a NSData boject and then assign it to the UIImageView
    UIImage *animalImage = UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[animal imageURL] ofType:@”jpg”;
    self.animalView.animalImage.image = animalImage;
    }
    [/code]



    Many thanks in advance

    Simon
    "We are what we repeatedly do, Excellence, then, is not an act but a habit". <i>Aristotle </i>
  • taylorsuktaylorsuk Posts: 11Registered Users
    edited February 2011
    I have got it to run without a error now however there i no image showing - have you just put them into your resources folder with the same name as the animal??

    cheers

    Simon
    "We are what we repeatedly do, Excellence, then, is not an act but a habit". <i>Aristotle </i>
Sign In or Register to comment.