0

My app can open the type of file it is supposed to work with when I double click the file, AND the app is already running. However, when the app is not running yet and i double click a file, the app starts, but it does not open the file. Why could that be?

The app delegate implements the methods:

-(void) application:(NSApplication *)sender openFiles:(NSArray *)filenames {
    for (NSString *name in filenames) {
        NSLog(@"Openning files");
        [self.topController addFileAtPath:name];
    }
}
-(BOOL) application:(NSApplication *)sender openFile:(NSString *)filename {
    NSLog(@"Openning file_");
    [self.topController addFileAtPath:filename];
    return YES;
}

4

1 回答 1

1

对于那些可能落入同一陷阱的人:

事实证明,上面的方法比“-applicationDidFinishLaunching:”更早被调用,我在其中进行了所有的应用程序初始化。我最终创建了一个“活动”标志(以显示我的应用程序是否已经启动),并将我所有的初始化逻辑放在一个单独的方法中。然后,在我的“...finishedLaunching”、“openFiles”和“openFile”中,我检查该标志是打开还是关闭,并相应地调用应用程序初始化方法:

@implementation DTVAppDelegate
BOOL alive = NO;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    if (!alive) {
        [self startApp];
    }
}
- (void) startApp {
    // init logic
    alive = YES;
}
-(void) application:(NSApplication *)sender openFiles:(NSArray *)filenames {
    if (!alive) {
        [self startApp];
    }
    for (NSString *name in filenames) {
        NSLog(@"Openning files");
        [self.topController addFileAtPath:name];
    }
}

于 2013-08-31T16:45:24.963 回答