Question

I have a document based application and when the application is closed, I need to load a url in a web browser. It's working fine, except that the NSDocument closes before this page can be loaded.

I would need it to wait 200 ms and then close the document.

I have found the NSTerminateLater but that is referred to the application, not the document. How can i do this?

this is what i have for now:

- (id)init
{
self = [super init];
if (self) {
    _statssent = NO;

    // Observe NSApplication close notification
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(_sendstats)
                                                 name:NSApplicationWillTerminateNotification
                                               object:nil];
}
return self;
}


- (void)_sendstats
{
if (!_statssent)
{
    _statssent = YES;

    if (hasuploaded == 1)
    {
            [self updatestatsUploads:0 progloads:1];
    }

 }
}

 - (void)close
{
[self _sendstats];

[super close];
}
Was it helpful?

Solution

Just before closing your document, you could issue a notification for which your application delegate could register as an observer.

When your application delegate receives the notification (which could convey the URL you need to open), a method on your application delegate could be called to open the URL for you.

You do this through the use of the instance of NSNotificationCenter that comes with every Cocoa application (more accurately, it's a singleton). Your document would issue the notification something like this:

NSDictionary *myUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"http://www.apple.com", @"MyURL", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotificationName" object:self userInfo:myUserInfo];

In your application delegate, probably in your -awakeFromNib method, you'd use something like this:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myURLOpenerMethod:) name:@"MyNotificationName" object:nil];

And somewhere in your application delegate, you could define that URL opener like this:

- (void)myURLOpenerMethod:(NSNotification *)notification
{
    NSString *urlString = [[notification userInfo] objectForKey:@"MyURL"];
    // use 'urlString' to open your URL here
}

You do not want to try using delays to get what you want. I promise you: That way madness lies.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top