iluvcapra iluvcapra - 1 month ago 17
Objective-C Question

Using an Objective-C protocol from my XPC Service in my Swift app

I have an app that is written in Swift, exclusively in Swift, never had an Obj-C class, has no Obj-C files or bridging headers.

In order to add some functionality without impacting my app's entitlements, I'm trying to add an XPC service -- I created this in Xcode, and the template for XPC services in Xcode is Obj-C only.

This is fine, I wrote my service out in Obj-C, but now when I want to call my service through

NSXPCConnection
I've gotta set up the interface to it, and to do that I need to refer, in my Swift app code, to the Obj-C protocol I wrote for my XPC target.

How do I do this? I can't just add the "Protocol.h" to my app target, it has no rule for a C header. All the other commentary I've found on this only seems to apply to linking to a protocol in a framework or a common target...

Answer

Okay, so I figured out a solution.

1) Deleted the boilerplate Obj-C service class and protocol files.

2) Wrote my protocol and vended object classes in swift. Made sure these could be exported to Obj-C.

3) Added the protocol.swift file to my app's target. Added #import "Service-Swift.h" to my service's main.m.

So now my XPC object is implemented in Swift, my object protocol is in swift, the only non-swift file is my XPC's main.

Comments