How do you make a Swift class available in an XPC service?

1.9k Views Asked by At

I'm attempting to rebuild Apple's XPC "lowerCase" sample code from Objective-C to Swift. I understand XPC will, but I'm relatively new to Swift and Objective-C interoperability.

When I use their exact sample code, which passes a String from the app to the target and back, it works. But when I try and replace that with my own custom class, Person, I get the error:

NSXPCConnection: ... connection on anonymousListener or serviceListener from pid 4133:

Exception caught during decoding of received selector upperCasePerson:withReply:, dropping incoming message.

Exception: Exception while decoding argument 0 (#2 of invocation):

Exception: decodeObjectForKey: class "CombineCycle.Person" not loaded or does not exist.

(The demo apps name is 'CombineCycle')

Person is a Swift class that inherits from NSObject and conforms to NSSecureCoding. It has a single property for name:String. It's accompanying .swift source file is included in the XPC target's build.

The exception seems pretty self-explanatory but I'm not sure which build settings I need to change, or which files I need to generate, to fix the underlying issue.

This is just a demo app. The main app was created as a Swift app and contains no Objective-C files. The XPC target was created using Xcode's new target feature which, in Xcode 11, generates an Objective-C skeleton. I replaced all of those files with Swift implementations.

// Person.swift (Included in both targets)

@objc public class Person : NSObject, NSSecureCoding  {
  var name:String?

  init(_ name:String) {
    self.name = name
  }

  public static var supportsSecureCoding: Bool {
    return true
  }

  public required init?(coder: NSCoder) {
    self.name = coder.decodeObject(forKey: "name") as? String
  }

  public func encode(with coder: NSCoder) {
    coder.encode(self.name, forKey: "name")
  }
}

Update

The XPC service is able to instantiate an instance of Person just fine, so the class is being included. The problem appears to be that NSXPCDecoder (a private class) is unable to deserialize it...

    __exceptionPreprocess + 250
    objc_exception_throw + 48
    _decodeObject + 1413
    -[NSXPCDecoder _decodeObjectOfClasses:atObject:] + 63
    _NSXPCSerializationDecodeInvocationObjectOnlyArgumentArray + 463
3

There are 3 best solutions below

3
kennyc On BEST ANSWER

The problems appears to be related to how NSCoder does archiving and unarchiving by relying on the classe's name. When the Person object is encoded on the application side, the name used is <ModuleName.ClassName>.

When attempting to deserialize that in the service, ModuleName is naturally wrong. Using @objc(Person) on the Person to set the Objective-C names appears to be working.

0
Lucas van Dongen On

I found it a lot easier to simply put all of the shared objects in a separate framework and import that framework everywhere I'm dealing with these objects. I never managed to fix this error, but it simply went away once the objects were inside a separate framework.

8
Joshua Kaplan On

You may find it quite a bit simpler to use SecureXPC instead which provides a pure Swift API. It uses Codable which is a very well support protocol within the Swift ecosystem.

I built this after finding the Objective-C API to be quite awkward and generally disliked how much the use of @objc ended up spreading through my codebase.

Under the hood it makes use of the XPC C framework; this is not a wrapper for the Objective-C API nor does it resemble the C framework. It's designed from the ground up for Swift.