Posted By

zingo on 05/26/11


Tagged

cocoa osx objc AppDelegate


Versions (?)

Checking If You App Has Been Run Before


 / Published in: Objective C
 

URL: http://programmersweb.blogspot.com/2011/05/checking-if-you-app-has-been-run-before.html

Do you need you app to do something when it is first run on a certain computer? Do you need to know if it has been run before? Enter the following code into your Application Delegate.

This code creates a BOOL that is created when the app is run. The if-else statement reads the value of the BOOL -- Yes or No. NOTE: You'll notice that if you run the app from xcode with the run button it says "Not run before". When you run it again it still says that. In order to truly test locate you apps Debug .app application then run that. Xcode clears out the data when you run it again. IT DOES WORK though, so don't worry if running you app in Xcode still gives you the Not Run Before result.

  1. -(void)awakeFromNib{
  2. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  3. BOOL hasRanBefore = [defaults boolForKey:@"hasRanBefore"];
  4. [defaults setBool:YES forKey:@"hasRanBefore"];
  5. [defaults synchronize];
  6.  
  7. if (hasRanBefore) {
  8. NSAlert *iRanBefore = [NSAlert alertWithMessageText:@"I Ran Before" defaultButton:@"OK" alternateButton:@"Cancel" otherButton:nil informativeTextWithFormat:@"This application has run before."];
  9. [iRanBefore runModal];
  10. } else {
  11. NSAlert *iDidNotRunBefore = [NSAlert alertWithMessageText:@"I Didn't Run Before" defaultButton:@"OK" alternateButton:@"Cancel" otherButton:nil informativeTextWithFormat:@"This application has not been run before."];
  12. [iDidNotRunBefore runModal];
  13. }

Report this snippet  

You need to login to post a comment.