如何在Xcode中正确布置独立的系统服务?

2 macos xcode cocoa objective-c

我最近有兴趣为OS X编写一些系统服务,但由于我没有应用程序来宣传服务,我必须求助于编写独立的系统服务.Apple关于系统服务的文档虽然简洁,但其关于独立服务的文档并不存在.

我有迄今是从捆绑包构建的Xcode项目,与两个来源HashifyService.hHashifyService.m.这是我的测试代码:

- (void) doServiceWork:(NSPasteboard *)pboard
            userData:(NSString *)userData
                 error:(NSString **)error {
  NSLog(@"Actually in the service now");
  NSString *pboardString;
  NSArray *types;

  NSLog(@"do test magic service! (pboard: %@, types: %@)", pboard, [pboard types]);


  NSString* outputString = @"It Worked";
  types = [NSArray arrayWithObject:NSStringPboardType];
  [pboard declareTypes:types owner:nil];
  [pboard setString:outputString forType:NSStringPboardType];
  [outputString release];
  return;
}
Run Code Online (Sandbox Code Playgroud)

这是NSServices我的条目Info.plist:

<dict>
<key>NSMenuItem</key>
<dict>
    <key>Menu item title</key>
    <string>HashifyTest</string>
</dict>
<key>NSMessage</key>
<string>doServiceWork</string>
<key>NSPortName</key>
<string>HashifyService</string>
<key>NSReturnTypes</key>
<array>
    <string>NSStringPboardType</string>
</array>
<key>NSSendTypes</key>
<array>
    <string>NSStringPboardType</string>
</array>
</dict>
Run Code Online (Sandbox Code Playgroud)

然后,我构建服务包并将其放置在~/Library/Services/适当检测到的位置,并且我可以选择使用该服务.但是,在激活服务时,会发生错误并记录到控制台:

..../Hashify.service/Contents/MacOS/Hashify: cannot execute binary file 
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

小智 8

你需要一个main()功能.这应该使用注册服务NSRegisterServicesProvider()并进入运行循环.那是在文档中.