Using protocols with XPC C API instead of dictionaries for sending and receiving messages

I have followed this post for creating a Launch Agent that provides an XPC service on macOS using Swift-

post link - https://rderik.com/blog/creating-a-launch-agent-that-provides-an-xpc-service-on-macos/

In the swift code the interface of the XPC service is defined by protocols which makes the code nice and neat. I want to implement the XPC service using C APIs for XPC, and C APIs send and receive messages using dictionaries, which need manual handling with conditional statements.

I want to know if its possible to go with the protocol based approach with C APIs.

Answered by DTS Engineer in 823023022

Why are you using the C API?

This matters because, if you’re using the C API because you can’t use Objective-C, then you also can’t use protocols.

Oh, and you tagged your thread with Swift, which indicates that you might be interested in the new low-level Swift API. It can use Swift’s Codable infrastructure, which might strike the right balance for your requirements. For links to docs about this, see XPC Resources.

Oh, and one last thing: If you’re using the C API because of some specific API that only works in that context, there are ways to bridge between the various APIs. Lemme know if that’s the case and we can explore your options.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

Why are you using the C API?

This matters because, if you’re using the C API because you can’t use Objective-C, then you also can’t use protocols.

Oh, and you tagged your thread with Swift, which indicates that you might be interested in the new low-level Swift API. It can use Swift’s Codable infrastructure, which might strike the right balance for your requirements. For links to docs about this, see XPC Resources.

Oh, and one last thing: If you’re using the C API because of some specific API that only works in that context, there are ways to bridge between the various APIs. Lemme know if that’s the case and we can explore your options.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I have certain restrictions on the language I can use, and C is the only option for me. I am not using the C API because of some specific API that only works in that context.

Thanks Quinn for the information.


K

Using protocols with XPC C API instead of dictionaries for sending and receiving messages
 
 
Q