-
-
Save unnamedd/10a79e0a433915c7694edf1441aca3c2 to your computer and use it in GitHub Desktop.
Components of XPC service.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
let delegate = MyServiceDelegate() | |
let listener = NSXPCListener.service() | |
listener.delegate = delegate | |
listener.resume() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class MyService: NSObject, MyServiceProtocol { | |
func upperCaseString(_ string: String, withReply reply: @escaping (String) -> Void) { | |
let response = string.uppercased() | |
reply(response) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class MyServiceDelegate: NSObject, NSXPCListenerDelegate { | |
func listener(_ listener: NSXPCListener, shouldAcceptNewConnection newConnection: NSXPCConnection) -> Bool { | |
let exportedObject = MyService() | |
newConnection.exportedInterface = NSXPCInterface(with: MyServiceProtocol.self) | |
newConnection.exportedObject = exportedObject | |
newConnection.resume() | |
return true | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
@objc public protocol MyServiceProtocol { | |
func upperCaseString(_ string: String, withReply reply: @escaping (String) -> Void) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import MyService | |
... | |
let connection = NSXPCConnection(serviceName: "com.matthewminer.MyService") | |
connection.remoteObjectInterface = NSXPCInterface(with: MyServiceProtocol.self) | |
connection.resume() | |
let service = connection.remoteObjectProxyWithErrorHandler { error in | |
print("Received error:", error) | |
} as? MyServiceProtocol | |
service?.upperCaseString("Hello XPC") { response in | |
print("Response from XPC service:", response) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment