0

I have a simple word game that I just put up on the App Store. The games progress is saved from session to session on my iPad and iPhone 7 plus but not on iPhone XS Max.

So, the code is working for saving game progress except for XS Max. Other XS Max phones as well as mine.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.


#if 0
    NSArray *puzzlesArray;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *docFile = [documentsDirectory stringByAppendingPathComponent:@"Puzzles.plist"];

    puzzlesArray = [[NSArray alloc] initWithContentsOfFile:docFile];
    if (!puzzlesArray) {
        NSString *thePath = [[NSBundle mainBundle] pathForResource:@"puzzles" ofType:@"plist"];

        puzzlesArray = [[NSArray alloc] initWithContentsOfFile:thePath];
    }
#endif


    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {

    [[NSNotificationCenter defaultCenter] postNotificationName:@"applicationWillResignActive" object:self];

}

- (void)applicationDidEnterBackground:(UIApplication *)application {

    [[NSNotificationCenter defaultCenter] postNotificationName:@"applicationDidEnterBackground" object:self];
}

Both of those with appropriate code in Game Controller.

- (BOOL)writeApplicationData:(NSData *) data toFile:(NSString *)docFile {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    if (!documentsDirectory) {

        NSLog(@"Documents directory not found!");

        return (NO);

    }

    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:docFile];


    return ([data writeToFile:appFile atomically:YES]);
}

- (void)applicationWillTerminate:(UIApplication *)application {

#if 0

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *docFile = [documentsDirectory stringByAppendingPathComponent:@"puzzles.plist"];

    NSData *pData = [[[NSData alloc] initWithContentsOfFile:docFile] autorelease];

    [self writeApplicationData:pData toFile:docFile];
#endif // if 0

}

- (NSData *)applicationDataFromFile:(NSString *)docFile {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:docFile];

    NSData *pData = [[NSData alloc] initWithContentsOfFile:appFile];

    return (pData);

}

@end

I have no code in applicationDidEnterBackground or applicationWillEnterForeground.

For the XS Max the games are saved for two days or so before reverting back to entry point.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Unrelated to your question but why are you posting a custom notification in `applicationWillResignActive` and `applicationDidEnterBackground`? iOS already provides standard notifications for those events that you can listed for. – rmaddy Oct 17 '19 at 03:00
  • I was advised to do that for another project years ago. – tj mclaughlin Oct 17 '19 at 04:23
  • There was never any reason to ever post your own notifications for something that already has its own notification. That was bad advice. – rmaddy Oct 17 '19 at 05:00
  • This delayed response is due to the fact that I've been trying different things. One which saved the game for a month before crapping out. Also, when I remove those notifications nothing is saved from one session to the other. Again, the iPad saves it all. The iPhone 7 was saving but it crapped out as well. I also have scoring data saved to the cloud. Could that be a conflict since I'm saving the game in appDelegate and saving just the scoring data in cloud??? – tj mclaughlin Feb 02 '20 at 02:17

0 Answers0