Restart your Cocoa application using Sparkle

原文连接:http://blog.mbcharbonneau.com/2010/06/03/restart-your-cocoa-application-using-sparkle/

It’s not uncommon for an OS X application to need to restart itself in certain unavoidable situations, such as hiding the dock icon. Most of the solutions you’ll find on the Internet rely on a command line helper app that waits for the parent application to finish exiting before launching it again. Although this isn’t hard to implement yourself, chances are you already have everything you need, buried inside the Sparkle framework.

Based on the Sparkle source code, here’s a quick way to restart any application that includes the Sparkle framework.

NSString *launcherSource = [[NSBundle bundleForClass:[SUUpdater class]]  pathForResource:@"relaunch" ofType:@""];
NSString *launcherTarget = [NSTemporaryDirectory() stringByAppendingPathComponent:[launcherSource lastPathComponent]];
NSString *appPath = [[NSBundle mainBundle] bundlePath];
NSString *processID = [NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]];

[[NSFileManager defaultManager] removeItemAtPath:launcherTarget error:NULL];
[[NSFileManager defaultManager] copyItemAtPath:launcherSource toPath:launcherTarget error:NULL];
	
[NSTask launchedTaskWithLaunchPath:launcherTarget arguments:[NSArray arrayWithObjects:appPath, processID, nil]];
[NSApp terminate:sender];

If you’re not using Sparkle, here’s a complete implementation of this idea you may find helpful.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值