3 * Copyright (c) 2016 Apple Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #import <Foundation/Foundation.h>
19 #import "BonjourPrefTool.h"
21 @interface ServiceDelegate : NSObject <NSXPCListenerDelegate>
24 @implementation ServiceDelegate
26 - (BOOL)listener:(NSXPCListener *)listener shouldAcceptNewConnection:(NSXPCConnection *)newConnection {
27 // This method is where the NSXPCListener configures, accepts, and resumes a new incoming NSXPCConnection.
28 (void)listener; // Unused
30 // Configure the connection.
31 // First, set the interface that the exported object implements.
32 newConnection.exportedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(BonjourPrefToolProtocol)];
34 // Next, set the object that the connection exports. All messages sent on the connection to this service will be sent to the exported object to handle. The connection retains the exported object.
35 BonjourPrefTool *exportedObject = [BonjourPrefTool new];
36 newConnection.exportedObject = exportedObject;
38 // Resuming the connection allows the system to deliver more incoming messages.
39 [newConnection resume];
41 // Returning YES from this method tells the system that you have accepted this connection. If you want to reject the connection for some reason, call -invalidate on the connection and return NO.
47 int main(int argc, const char *argv[])
52 // Create the delegate for the service.
53 ServiceDelegate *delegate = [ServiceDelegate new];
55 // Set up the one NSXPCListener for this service. It will handle all incoming connections.
56 NSXPCListener *listener = [NSXPCListener serviceListener];
57 listener.delegate = delegate;
59 // Resuming the serviceListener starts this service. This method does not return.