MB_iOSDeveloper
MB_iOSDeveloper

Reputation: 4198

How do I store permanent data

I am new to iOS development and have the following problem.

EXPLANATION: I have an app which consists of several mini games. Each mini game is available through in app purchase. However the mini game availability is saved in a BOOL variable like this:

 _miniGamesArray = [[NSMutableArray alloc] init]; 
 NSMutableDictionary *mutDictMg0 = [NSMutableDictionary dictionaryWithCapacity:3];
 [mutDictMg0 setValue:[self.gameAvailabilityMutableArray objectAtIndex:0] forKey:@"GameAvailable"];
[_miniGamesArray addObject:mutDictMg0];

PROBLEM:

Each time I start the app the game availability is checked from the self.gameAvailabilityMutableArray which is set to:

- (NSMutableArray *)gameAvailabilityMutableArray
{
    if (!_gameAvailabilityMutableArray)
    {
        //[_gameAvailabilityMutableArray addObjectsFromArray:@[@1,@0,@0,@0    ,@0,@0,@0,@0]];
        _gameAvailabilityMutableArray = [[NSMutableArray alloc] initWithArray:@[@1,@1,@1,@1    ,@1,@1,@1,@1]];


    }
    return _gameAvailabilityMutableArray;
}

When the customer buys a mini game I want the array to be set to (example):

 @[@1,@1,@0,@0    ,@0,@0,@0,@0]]

TRIED SOLUTIONS: I tried to implement the array by calling the iTunes server and writing the data. However, the time to recieve the request is greater then the app loading time. The second problem arrises if there is no internet connection, then the app crashes.

I also tried using .plist files. I don't know why but writing to the plist file doesn't change it's contest all the time! Some times it works , sometimes it doesn't... Sometimes the app loads the values correctly sometimes it mixes them with the last values.

QUESTION:

Is there a way to store permanent app data which is being checked when the app loads beside plists?

Thank you for your time.

Upvotes: 1

Views: 1086

Answers (2)

Renish Dadhaniya
Renish Dadhaniya

Reputation: 10752

  save your data in NSUserDefaults.then use below conditions for app start first time or handle another conditions.

 BOOL iSFirstTime = [[NSUserDefaults standardUserDefaults] boolForKey:@"AppStartFirstTime"];
    if (!iSFirstTime) {
        NSLog(@"Application start first time");
        [[NSUserDefaults standardUserDefaults] setValue:@"ImgDownloaded" forKey:@"ProductIDDef"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"AppStartFirstTime"];
    }else{
        //   NSLog(@"we are here");
        if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"ProductIDDef"] isEqualToString:@"ImgDownloaded"]) {
            NSLog(@"All transaction and Image Downloading has been perform successfully");
        }else{
//If any network problem or any thing else then handle here.
        }

    }

}

Upvotes: 1

Ritu
Ritu

Reputation: 671

You can save the data in NSUserDefaults using the following code:

       [[NSUserDefaults standardUserDefaults] setObject:_gameAvailabilityMutableArray forKey:@"gameArray"];
       [[NSUserDefaults standardUserDefaults] synchronise];

and you can retrieve the array using

_gameAvailabilityMutableArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"gameArray"];

Upvotes: 1

Related Questions