Fun with XPC

Ali Pourhadi
3 min readSep 6, 2019

For a long time I was curious to learn how IPC works on OSX. if you do search about it most of the results are related to how jailbreak? how to make an exploit? and so on. But i could not find a simple tutorial to learn how actually this things work? how I can make my own service? In this tutorial I am not going to talk about “launchd” or “Mach Messages” , “DO” , “Pasteboard”, …. We are just going to make simple service and see how it can be cool and useful to break your app to different services.

So what is XPC?

https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingXPCServices.html

XPC is a way for interprocess communication ( IPC ). what that means? let’s say we break our app to one main application and couple of services. let’s say these services all always ( technically it’s does not happen ) running as separate apps. There are many ways to cominucate between them. XPC is one them.

We are going to make a very simple one to see how this thing works. Create a new Cocoa App ( I made it in Objective-C but there many samples on github base on Swift ) . Then add new target and select XPC.

While creating XPC target be careful about Bundle Identifier we will need it later. In my case it’s “pourhadi.ali.xpctest”.

So in my case these 5 files has been added into project. let’s begin with main.m

basically you will find how your service will be initialized and listen to requests that comes and will answer to them. The important part of file is our ServiceDelegate which defines to which requests we have to listen and passing our service object to. exportedInterface says through which kind of interface others can talk to me in our example is gonna be “xpctestProtocol”. That make sense. your app and your service should talk in a same language and then what we passing as exported object is the object that is going to handle our request. In our example it’s xpctest whom implementing xpctestProtocol.

let’s take a look into xpctest.m file

it’s just a simple method whom turn a string to uppercase string and return it inside of a block.

Now, our service is ready to use. we have to make a connection from our host app to our service app.

It’s just a sample, so please don't judge me for putting eveything in viewcontroller :)).

Basically we need a connection to our server. This is the point that we need our bundle id. It’s gonna be our service name. Exported object in service is gonna be our remote object here. We need to know through which interface we can talk to our remote object. We can also track if our connection interrupted or did not happen. You can define your call backs. So at this point I guess it’s clear that the connection between our app and service is not always there.

There you go. run you app and pass a string and get the uppercased one. this is a very simple easy XPC connection.

--

--