Relaunch an application

Some tasks, e.g. restoring an application’s data files from a backup should be performed while the application is not running to make sure there won’t be any concurrent access to the files. A desirable behaviour would be: Quit the application, perform the work and finally relaunch the application – just as Sparkle does when installing an update.

I’ve created a simple but well-working solution for that problem. My solution is using a small helper application which performes the work which has to be done while the main application is not running and relaunches it afterwards. You can download the sample project to try it out by yourself.

The RelaunchHelper is just an ordinary binary which has to be launched by your application just before it terminates itself. Of course, you can pass arguments to the RelaunchHelper. In the example project the path to the main application’s bundle is the sole argument. After invoking the helper, your application should terminate itself and let te helper take over.
I let the helper app sleep for a few seconds befor it relaunches the application to ensure, all files opened by the main application (in this example this is not an issue) are properly closed.

These are the steps you have to follow to create a project:

  • Create a target for the helper
  • Add all needed source files to that target
  • Make that target a direct dependency of your main app’s target to make sure the helper is also build when you build your main app
  • Create a Copy Files build phase for your main app’s target and add the helper to it. This is necessary to make the helper binary appear in your app’s bundle
  • Build and go 🙂
  • So, that’s my solution to the relaunch issue. Maye it’s not the most elegant way to do this. If you have a better solution, let me know. I really appreciate any comment.

    Update:
    A much better way to implement a relaunch helper has been provided by Cédric Luthi in his Blog post.
    Please check it out.

    This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

    4 Responses to Relaunch an application

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.