简体   繁体   中英

Execute code before state restoration happens (macOs)

I'm writing a document-based application for Mac in Swift 4, which, according to my client needs, has to show a licensing window were the user will provide its license key.

I show this window at the applicationWillFinishLaunching() method. While this window is active, the state restoration methods run in the background and load previous nsdocuments, or create empty ones if there were no previous. I want to avoid that, I want to be able to choose when restoration and behaving like a document based app starts.

I've tried to intercept the launching of the app in the appDelegate method applicationShouldOpenUntitledFile(_ sender: NSApplication) but I was not successful. Then I've read here that this method doesn't gets called if application state restoration is active. To confirm that, I deactivated the restoration, and then last documents or empty documents were not loaded / created anymore as expected. Great!

But then, I loose the nice restoration functionality.

I'm wondering if there is a better way to do this: displaying a licensing screen in a document-based app, halting the restoration methods, and manually calling them after app is licensed.

Thanks

This is Objective C, but this is how I did it to show a dialog in which the user has to accept some license conditions:

In my AppDelegate, I got a property licenseDialogOpen which is set to false when the app starts.

@synthesize licenseDialogOpen;

- (instancetype)init {
    self = [super init];
    if (self) {
        self.licenseDialogOpen = FALSE;
    }
    return self;
}

In my Document class I overwrote windowControllerDidLoadNib

- (void)windowControllerDidLoadNib:(NSWindowController *)windowController {
    [super windowControllerDidLoadNib:windowController];

    AppDelegate *appDelegate = [NSApp delegate];

    if (!appDelegate.licenseDialogOpen) {
        NSAlert *alert = [[NSAlert alloc] init];
        [alert setMessageText:NSLocalizedString(@"License conditions and disclaimer:", nil)];
        [alert setInformativeText:NSLocalizedString(@"License bla bla disclaimer bla bla bla", nil)];
        [alert setAlertStyle:NSAlertStyleWarning];
        [alert addButtonWithTitle:NSLocalizedString(@"Accept", nil)];
        [alert addButtonWithTitle:NSLocalizedString(@"Quit", nil)];

        [alert.window makeFirstResponder:[[alert buttons] firstObject]];

        appDelegate.licenseDialogOpen = TRUE;
        NSModalResponse answer = [alert runModal];
        if (answer != NSAlertFirstButtonReturn) {
            for (NSWindow *window in [NSApplication sharedApplication].windows) {
                [window close];
            }
            [NSApp terminate:self];
        }
    }
}

So the first document window which is opened shows the modal dialog and quits the app when the user does not accept.

You can add a NSTextField to a NSAlert in order to ask for the license key.

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