1

is there a way to run a code only once?Not for each usage.When the user download the app and runs it, the code will run only once and not anymore.

i tried to create a integer with initial 0 and save it to nsuerdefaults.

and made this special code in a if statement check, if its 0 enter the code but if its not 0 don't enter it, and in the if statement i incremented 0 and saved it so it won't ever again enter it.

but it didn't work out

 NSInteger temp = [[NSUserDefaults standardUserDefaults] integerForKey:@"forOnce"];


if (temp==0) {

int k=0;

if (NSClassFromString(@"CTTelephonyNetworkInfo"))
{

CTTelephonyNetworkInfo *netInfo =[[CTTelephonyNetworkInfo alloc]init];
CTCarrier *carrier =[netInfo subscriberCellularProvider];


testUser[@"Carrier"]=[carrier carrierName];
//NSLog(@"carrier bu %@",[carrier carrierName]);




 }

    temp++;


    [[NSUserDefaults standardUserDefaults] setInteger:k forKey:@"forOnce"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    }

the code was something like that

judge
  • 195
  • 1
  • 4
  • 14
  • Temp == 0 when you load it the first time. So you have to place k=1 (and not k=0). Because when you save k=0 in user defaults, you will retrieve it as 0 again the next time in Temp. – Painted Black Jul 03 '14 at 08:28
  • What if user changes device and reinstalls the app onto their new device? :D – Zhang Jul 03 '14 at 08:29
  • @Zhang haha that would cuz a little bit of misinformation :) – judge Jul 03 '14 at 08:30
  • @PaintedBlack i didn't quite understand what you are trying to tell me – judge Jul 03 '14 at 08:30
  • save the `k` into the keychain or/and the iCloud folder. – holex Jul 03 '14 at 08:32
  • 1
    His if() condition is checking for tempt but he is saving "k" into NSUserDefaults, tempt will always be 0, the condition is always true. I think he needs to store tempt into NSUserDefaults instead of "k". – Zhang Jul 03 '14 at 08:34
  • I'm trying to tell you that you should store 1 in NSUserDefaults. Now you are storing k (which is 0). Solution: store temp, or set k=1 and store k. – Painted Black Jul 03 '14 at 08:37

2 Answers2

2
if(![[NSUserDefaults standardUserDefaults] boolForKey:@"EXECUTE_ALREADY"])
{
        NSLog(@"ONCE");
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"EXECUTE_ALREADY"];
        [[NSUserDefaults standardUserDefaults] synchronize];
}

The basic idea is same as yours. It works.

Ryan
  • 4,799
  • 1
  • 29
  • 56
  • well, it works but the code never runs EVER, i got 3 test device and after the first run it gets the save from nsuserdefaults – judge Jul 03 '14 at 10:37
0
Try this method in AppDelegate.h

@property(nonatomic,assign)BOOL isFirst;


     - (BOOL) isFirstRun
    {
        Defaults = [NSUserDefaults standardUserDefaults];
        if ([Defaults objectForKey:@"isFirstRun"])
        {
            //if application running in second time
            isFirst=NO;
             NSLog(@"isSecondRun isfirst:%d",isFirst);
            return NO;
        }
        //if application running in first time
        isFirst=YES;
        NSLog(@"isFirstRun isfirst:%d",isFirst);

        [Defaults setObject:[NSDate date] forKey:@"isFirstRun"];
        [[NSUserDefaults standardUserDefaults] synchronize];

        return YES;
    }



and check 

NSLog(@"isfirst:%d",[self isFirstRun]);

if(isFirst==1)
{
//Do your operations.
}
Iphonenew
  • 299
  • 2
  • 11