重新启动可可应用程序

dev*_*vid 5 cocoa process objective-c

我有一个应用程序,它检查其命令行参数并将值存储在持久存储中.其中一个是密码,我不想让人们看到'ps'和朋友.我目前正在研究的方法是,在我存储了我需要的值之后,在没有命令行参数的情况下重新启动进程.我天真的方法就是这样,其中args [0]是应用程序的路径:

NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:[args objectAtIndex:0]];
[task launch];
[task release];
[NSApp terminate:nil];
Run Code Online (Sandbox Code Playgroud)

孩子跑了.然而,当我的应用程序被终止时,孩子似乎不是孤儿,而是被卡住了.我离开这个吗?

更多信息:所以看来,当我调用[NSApp terminate:nil]时,启动的NSTask卡住了,但是如果我只是退出()那么它工作正常.但是,如果我这样做,我担心开放的东西(钥匙串,plist等)会处于不良状态.

请注意,许多示例代码都是关于某些类似监视程序的进程,它会在需要时重新启动单独的进程.我正在尝试重新启动已在同一进程内运行的当前进程.

Car*_*rum 10

网上有很多例子,但是这个例子(也在下面)看起来它有你需要的所有代码.还有更详细的解释.

// gcc -Wall -arch i386 -arch ppc -mmacosx-version-min=10.4 -Os -framework AppKit -o relaunch relaunch.m

#import <AppKit/AppKit.h>

@interface TerminationListener : NSObject
{
    const char *executablePath;
    pid_t parentProcessId;
}

- (void) relaunch;

@end

@implementation TerminationListener

- (id) initWithExecutablePath:(const char *)execPath parentProcessId:(pid_t)ppid
{
    self = [super init];
    if (self != nil) {
        executablePath = execPath;
        parentProcessId = ppid;

        // This adds the input source required by the run loop
        [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(applicationDidTerminate:) name:NSWorkspaceDidTerminateApplicationNotification object:nil];
        if (getppid() == 1) {
            // ppid is launchd (1) => parent terminated already
            [self relaunch];
        }
    }
    return self;
}

- (void) applicationDidTerminate:(NSNotification *)notification
{
    if (parentProcessId == [[[notification userInfo] valueForKey:@"NSApplicationProcessIdentifier"] intValue]) {
        // parent just terminated
        [self relaunch];
    }
}

- (void) relaunch
{
    [[NSWorkspace sharedWorkspace] launchApplication:[NSString stringWithUTF8String:executablePath]];
    exit(0);
}

@end

int main (int argc, const char * argv[])
{
    if (argc != 3) return EXIT_FAILURE;

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    [[[TerminationListener alloc] initWithExecutablePath:argv[1] parentProcessId:atoi(argv[2])] autorelease];
    [[NSApplication sharedApplication] run];

    [pool release];

    return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)