title: Xcode+Swift+XPC: How to start and deploy a Swift XPC target on MacOS
body: <p><strong>DISCLAIMER:</strong> I am relatively new to MacOS/Xcode</p>
  
  <p>I want to build a simple XPC Launch Agent in Swift (ie: in <code>~/Library/LaunchAgents</code>) but I could not find much documentation.<br>
  I started with Xcode XPC template but I do not know if it was a good idea for my Swift project.<br>
  I notice I should also have <code>~/Library/LaunchAgents/com.demo.myservice.plist</code></p>
  
  <p>Versions:<br>
  - MacOS: 10.13.2<br>
  - Xcode: 9.2</p>
  
  <p>Instruction to create the Xcode XPC Project:</p>
  
  <ol>
  <li>File > New project</li>
  <li>I chose the MacOS template: XPC</li>
  <li>I create the bundle ‘com.demo.myservice’</li>
  <li>It creates me an Objective-C project. So I delete all files (ie: <code>myserviceProtocol.h</code>, <code>myservice.h</code>, <code>myservice.m</code>, <code>main.m</code> and <code>Info.plist</code></li>
  <li>Create the files:</li>
  </ol>
  
  <p><em>myserviceProtocol.swift</em></p>
  
  <pre><code>import Foundation
  
  @objc(myserviceProtocol) protocol myserviceProtocol {
      func ping()
  }
  </code></pre>
  
  <p><em>myservice.swift</em></p>
  
  <pre><code>import Foundation
  
  class myservice : NSObject, myserviceProtocol {
      func ping() {
          print("ping")
      }
  }
  </code></pre>
  
  <p><em>main.swift</em></p>
  
  <pre><code>import Foundation
  
  class ServiceDelegate : NSObject, NSXPCListenerDelegate {
      func listener(_ listener: NSXPCListener, shouldAcceptNewConnection newConnection: NSXPCConnection) -&gt; Bool {
          newConnection.exportedInterface = NSXPCInterface(with:myserviceProtocol.self)
          let exportedObject = myservice()
          newConnection.exportedObject = exportedObject
          newConnection.resume()
          return true
      }
  }
  
  // Create the listener and resume it:
  //
  let delegate = ServiceDelegate()
  let listener = NSXPCListener.service()
  listener.delegate = delegate;
  listener.resume()
  </code></pre>
  
  <p><em>Info.plist</em></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
  &lt;plist version="1.0"&gt;
      &lt;dict&gt;
          &lt;key&gt;KeepAlive&lt;/key&gt;
          &lt;true/&gt;
          &lt;key&gt;Label&lt;/key&gt;
          &lt;string&gt;com.demo.myservice&lt;/string&gt;
          &lt;key&gt;ProgramArguments&lt;/key&gt;
          &lt;array&gt;
              &lt;string&gt;myservice&lt;/string&gt;
          &lt;/array&gt;
          &lt;key&gt;RunAtLoad&lt;/key&gt;
          &lt;true/&gt;
      &lt;/dict&gt;
  &lt;/plist&gt;
  </code></pre>
  
  <ol start="6">
  <li><p>I build it:
  <a href="https://i.stack.imgur.com/vYJil.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vYJil.png" alt="enter image description here"></a></p></li>
  <li><p>I copied <code>Info.plist</code> into ~/Library/LaunchAgents/ : <code>cp ~/Documents/myservice/myservice/Info.plist ~/Library/LaunchAgents/com.demo.myservice.plist</code><br>
  I retrieve my userid with <code>id -u</code></p></li>
  </ol>
  
  <p>And then I try to execute it from the command line (as it does not seem to do anything from Xcode):  </p>
  
  <pre><code>sudo launchctl debug user/501/com.demo.myservice /Users/olivier/Library/Developer/Xcode/DerivedData/myservice-hbwefcgibyqbajguvblgcmxsnrmd/Build/Products/Debug/myservice.xpc
  Configuration failed: 113: Could not find specified service
  Could not find service "com.demo.myservice" in domain for uid: 501
  </code></pre>
  
  <p>I am not really sure of what I am doing. Was I right to use <code>XPC</code> template to create my swift XPC.</p>