Mac Developer Library

Developer

Foundation Framework Reference NSXPCListener Class Reference

Options
Deployment Target:

On This Page
Language:

NSXPCListener

Inheritance


Conforms To


Import Statement


Swift

import Foundation

Objective-C

@import Foundation;

Availability


Available in OS X v10.8 and later.

The NSXPCListener class and its delegate are responsible for waiting for new incoming connections, configuring them, and accepting or rejecting them.

Each XPC service, launchd agent, or launchd daemon typically has at least one NSXPCListener object that listens for connections to a specified service name. Each listener must have a delegate that conforms to the NSXPCListenerDelegate protocol. When the listener receives a new connection request, it creates a new NSXPCConnection object, then asks the delegate to inspect, configure, and resume the connection object by calling the delegate's listener:shouldAcceptNewConnection: method.

  • Returns an endpoint object that may be sent over an existing connection.

    Declaration

    Swift

    var endpoint: NSXPCListenerEndpoint { get }

    Objective-C

    @property(readonly, retain) NSXPCListenerEndpoint *endpoint

    Discussion

    The receiver of the endpoint can use this object to create a new connection to this NSXPCListener object. The resulting NSXPCListenerEndpoint object uniquely names this listener object across connections.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.8 and later.

  • Initializes a listener in a LaunchAgent or LaunchDaemon which has a name advertised in a launchd.plist file.

    Declaration

    Swift

    init(machServiceName name: String)

    Objective-C

    - (instancetype)initWithMachServiceName:(NSString *)name

    Discussion

    For example, you might use this in an agent launched by launchd with a launchd.plist contained in ~/Library/LaunchAgents, or a daemon launched by launchd with a launchd.plist contained in /Library/LaunchDaemons.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.8 and later.

  • Invalidates the listener.

    Declaration

    Swift

    func invalidate()

    Objective-C

    - (void)invalidate

    Discussion

    After calling this method, no more connections are created. Once a listener is invalidated it may not be resumed or suspended.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.8 and later.

  • Starts processing of incoming requests.

    Declaration

    Swift

    func resume()

    Objective-C

    - (void)resume

    Discussion

    All listeners start suspended and must be resumed before they begin processing incoming requests.

    If called on the serviceListener object, this method never returns. Therefore, you should call it as the last step inside the XPC service's main function after setting up any desired initial state and configuring the listener itself.

    If called on any other NSXPCListener, the connection is resumed, and the method returns immediately.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.8 and later.

  • Suspends the listener.

    Declaration

    Swift

    func suspend()

    Objective-C

    - (void)suspend

    Discussion

    Suspends and resumes must be balanced before the listener may be invalidated.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.8 and later.

  • Returns a new anonymous listener connection.

    Declaration

    Swift

    class func anonymousListener() -> NSXPCListener

    Objective-C

    + (NSXPCListener *)anonymousListener

    Discussion

    Other processes can connect to this listener by passing this listener object's NSXPCListenerEndpoint to the initWithListenerEndpoint: method of an NSXPCConnection object.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.8 and later.

  • Returns the singleton listener used to listen for incoming connections in an XPC service.

    Declaration

    Swift

    class func serviceListener() -> NSXPCListener

    Objective-C

    + (NSXPCListener *)serviceListener

    Discussion

    Calling the resume method on the returned object starts the listener and never returns. This method is typically called at the end of your main function.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.8 and later.

  • delegate delegate Property

    The delegate for the listener.

    Declaration

    Swift

    unowned(unsafe) var delegate: NSXPCListenerDelegate?

    Objective-C

    @property(assign) id< NSXPCListenerDelegate > delegate

    Discussion

    If no delegate is set, all new connections are rejected. See the documentation for NSXPCListenerDelegate for implementation details.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.8 and later.