0

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];
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
sharkyenergy
  • 3,842
  • 10
  • 46
  • 97

1 Answers1

1

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.

Extra Savoir-Faire
  • 6,026
  • 4
  • 29
  • 47
  • thanks, problem is that the app closes before the page has fully loaded. because once it has set the url to load inside the webview he is happy and has done its job. but i need to wait until the webpage logs the loading of the page. would it be wise to add something like `while([statsView isLoading]){}`? – sharkyenergy Mar 01 '13 at 20:42
  • 1
    Set your instance of `WebView` to use a delegate that conforms to the `WebFrameLoadDelegate` protocol. In that delegate, implement the delegate method `-webView:didFinishLoadForFrame:`; this will notify you when the page has finished loading, and you can quit accordingly. – Extra Savoir-Faire Mar 01 '13 at 20:48
  • uhm.. for some reason the method -webView:didFinishLoadForFrame: is never called. i made the connection with FrameLoadDelegate to file's owner and have obviously implemented the webkit. but it just isnt called. any idea why? – sharkyenergy Mar 01 '13 at 21:05
  • Is `File's Owner` your application delegate? This is important; you need to make sure that you've made the connection with your application delegate, not the instance of NSApplication itself. – Extra Savoir-Faire Mar 01 '13 at 21:11
  • isnt the connection to be made with the DOCUMENT instead of the appDelegate for a document based application? – sharkyenergy Mar 01 '13 at 21:16
  • If you have the WebView instance as part of the document, then yes. I didn't understand that the web view was associated with the document. – Extra Savoir-Faire Mar 01 '13 at 21:18
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/25389/discussion-between-trudyscousin-and-just-me) – Extra Savoir-Faire Mar 01 '13 at 21:24