简体   繁体   中英

How to send/save data when user quits OS X application

I'm working on an OS X application after having worked almost exclusively on iOS apps. If the user quits, say by pressing Command-Q, how can I store some data locally and send some data to my server before the app truly goes out of memory?

On iOS I typically launch a background event when I detect the app going into the background. I've tried listening for "ApplicationWillTerminate" but it doesn't seem to afford me the time to send data.

Should I be intercepting the menu action and performing my work before quitting?

I realize this is a pretty basic question, but my Google-Foo has not led me to a straightforward answer.

applicationWillTerminate is indeed the method to clean up things before the app quits.

Alternatively for an asynchronous way implement applicationShouldTerminate , start you task to store and send the data asynchronously and return NSTerminateLater . When the task is completed call

[NSApp replyToApplicationShouldTerminate:YES];

Here a snippet as example

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
    [self startTaskToSendDataWithCompletion:^() {
        [NSApp replyToApplicationShouldTerminate:YES];
     }];

     return NSTerminateLater;  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM