SwiftyXPC - a wrapper for Apple’s XPC interprocess communication library that gives it an easy-to-use, idiomatic Swift interface.

Last update: May 7, 2022

SwiftyXPC

Hmm, what’s this?

SwiftyXPC is a wrapper for Apple’s XPC interprocess communication library that gives it an easy-to-use, idiomatic Swift interface.

But there’s already NSXPCConnection!

Well, yes, but with its protocol-and-proxy-based interface, it’s far better suited to Objective-C than Swift. Using NSXPCConnection from Swift has always felt somewhat awkward, and with the advent of Swift Concurrency, it’s even worse, given that everything has to be wrapped in withCheckedThrowingContinuation blocks. NSXPCConnection has also tended to be behind libxpc in certain important ways—notably, in the ability to verify the code signature of a remote process via an audit token.

By contrast, SwiftyXPC:

  • Offers a fully Swift Concurrency-aware interface. Use try and await to call your helper code with no closures necessary.
  • Gives you a straightforward interface for your helper functions; take an argument, return a value async. No fussing around with Objective-C selectors and reply blocks.
  • Built around the Codable protocol, so you can use any types you want for the argument and return value, as long as both types conform to Codable, and it Just Works™. Error types are preserved as well, if they are Codable and you register their domains with the shared XPCErrorRegistry object.
  • Only links against XPC and Security (the latter for the code signature validation), so there’s no need to link Foundation into your app, deal with Objective-C bridging magic, or involve the Objective-C runtime at all (excluding any places where XPC or the Security framework may be using it internally).

But I want to support older macOS versions! Using Swift Concurrency means that it requires macOS 12!

Actually, it turns out that’s not true anymore! With Xcode 13.2, Swift concurrency now works all the way back to macOS 10.15 “Catalina”, and consequently, so does this library.

What’s the license on this?

MIT.

GitHub

https://github.com/CharlesJS/SwiftyXPC
You might also like...

The QuoteKit is a Swift framework to use the free APIs provided by Quotable created by Luke Peavey.

The QuoteKit is a Swift framework to use the free APIs provided by Quotable created by Luke Peavey.

QuoteKit The QuoteKit is a Swift framework to use the free APIs provided by Quotable created by Luke Peavey. It uses the latest async/await syntax for

May 7, 2022

How to use swiftlint to identify unused code or unused imports in a Swift codebase

Swift compilation database This repository demonstrates how to use swiftlint to identify unused code or unused imports in a Swift codebase. How to run

Feb 10, 2022

Sonic language: Heavily inspired by Swift, but compiles to C so you can use it anywhere.

Sonic language: Heavily inspired by Swift, but compiles to C so you can use it anywhere.

Sonic Sonic programming language: Heavily inspired by Swift, but compiles to C so you can use it anywhere. Brought to you by Chris Hulbert and Andres

Apr 8, 2022

A sample app demonstrates how to use Atlantis on Swift Playground (iOS/macOS)

A sample app demonstrates how to use Atlantis on Swift Playground (iOS/macOS)

Atlantis with Swift Playground A sample app demonstrates how to use Atlantis on

Dec 22, 2021

Use this template as a starting point for any Swift 5 module that you want other people to include in their projects

Swift 5 Module Template Use this template as a starting point for any Swift 5 mo

Dec 28, 2021

A Collection of PropertyWrappers to make custom Serialization of Swift Codable Types easy

CodableWrappers Simplified Serialization with Property Wrappers Move your Codable and (En/De)coder customization to annotations! struct YourType: Coda

May 16, 2022

Mimicrated views and controls to native Apple appearance.

Mimicrated views and controls to native Apple appearance.

Mimicrated views and controls to native Apple appearance. If you have any ideas of what elements can be added, let me know. Below you will see previews of all the elements and how to use them.

May 17, 2022

A apple search ads attribution plugin for flutter

A apple search ads attribution plugin for flutter

Oct 27, 2021

An Apple Watch remake of the Poketch from Pokemon Diamond and Pearl made with SwiftUI

An Apple Watch remake of the Poketch from Pokemon Diamond and Pearl made with SwiftUI

Apple Watch Poketch What is it? It's an Apple Watch remake of the "Poketch" from Pokemon Diamond and Pearl made with SwiftUI! Check out the YouTube vi

Nov 19, 2021
Comments
  • 1. How to work with `FileDescriptor`?

    FIrst of all, this framework is awesome! Apple should adopt the library as the official SDK (so they can also fixing the non type safe interface currently we are using).

    I'm wondering what is the correct way to handle FileDescriptor.

    The problem is, the current API only allows us to return a response, where the fd is not sent yet so we cannot close it immediately (or in defer). However, there is no way to know when the response is sent (or queued) so we cannot close the fd properly.

    If we are dealing with xpc_fd_create directly, then it is quite easy as it calls dup internally and managed the owned fd afterwards. I think we also need to do similar things using XPCFileDescriptor (even for 12.0+). XPCFileDescriptor should own the fd and close it after calling xpc_fd_create. User can still use FileDescriptor as long as they want to close the fd themselves.

    Reviewed by zhuhaow at 2022-01-15 10:48
  • 2. Example code snippet / question

    Thanks a ton for your work on this. From what I've been able to understand, this looks like a massive improvement on doing this via the NSXPCConnection route.


    What exactly would a simple XPC Service/client (macOS application) look like?

    Right now I have the main.swift file within an XPC Service project within the macOS app. What would be the best/most appropriate way to structure the service there that (for example) accepts a string from a client and returns it capitalized via its response?

    On the client side, what I imagine it would look like is that you'd setup the connection (in a struct?) and then activate the connection in applicationDidFinishLaunching within the AppDelegate but I'm not sure and was hoping to get some guidance on that.

    Reviewed by capaldo at 2022-04-26 20:00
The Git interface you've been missing all your life has finally arrived.
The Git interface you've been missing all your life has finally arrived.

GitUp Work quickly, safely, and without headaches. The Git interface you've been missing all your life has finally arrived. Git recently celebrated it

May 17, 2022
Are you sure the chemical compounds of your daily use are 100% safe? Use Chem-Aware, identify them right now!
Are you sure the chemical compounds of your daily use are 100% safe? Use Chem-Aware, identify them right now!

View Project On Devpost: Built With: PubChem's REST API How To Install Chem Aware: Prerequiste: Latest Version of Xcode and Simulators installed The a

Jan 4, 2022
Easy-to-use segues in SwiftUI, allowing for presenting views using common UIKIt Segue types - push, modal and popover
Easy-to-use segues in SwiftUI, allowing for presenting views using common UIKIt Segue types - push, modal and popover

Easy-to-use segues in SwiftUI, allowing for presenting views using common UIKIt Segue types - push, modal and popover

Apr 16, 2022
A Swift wrapper around the CoreSymbolication private framework on macOS.

CoreSymbolication provides a very powerful system for looking up and extracting symbolic information from mach-o executables, dyld shared caches, and dSYMs.

Nov 9, 2021
A Swift wrapper around the JSONbin.io API
A Swift wrapper around the JSONbin.io API

A Swift wrapper around the JSONbin.io API

Dec 16, 2021
Apple SMC library & tool

SMCKit An Apple System Management Controller (SMC) library & command line tool in Swift for Intel based Macs. The library works by talking to the Appl

May 22, 2022
Powerful property wrapper to back codable properties.

BackedCodable Powerful property wrapper to back codable properties. Why Swift's Codable is a great language feature but easily becomes verbose and req

May 12, 2022
A property wrapper to enforce that closures are called exactly once!

A property wrapper that allows you to enforce that a closure is called exactly once. This is especially useful after the introduction of SE-0293 which makes it legal to place property wrappers on function and closure parameters.

May 16, 2022
ExtraLottie is a SwiftUI wrapper of Lottie-iOS

?? What is ExtraLottie? ExtraLottie is a SwiftUI wrapper of Lottie-iOS. ℹ️ Info Currently ExtraLottie supports custom loop mode, LottieLoopMode, start

May 9, 2022
A collection of Swift Tutorials built with Apple's DocC.

Swift Tutorials This is not a package, it's just a bunch of tutorials This project uses Apple's DocC (Documentation Compiler) to create a set of Swift

Apr 15, 2022