xcode Helper mac app(登录项),无法与
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12228159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Helper mac app (Login Item), unable to communicate with
提问by strange
I've done exactly as the Sandboxed Helper App example provided by Apple and all seems to be running fine. I'm able to successfully create a NSXPCConnection
object and also get back my remote object (via remoteObjectProxyWithErrorHandler
).
我已经完全按照 Apple 提供的 Sandboxed Helper App 示例完成了,并且一切似乎都运行良好。我能够成功创建一个NSXPCConnection
对象并取回我的远程对象(通过remoteObjectProxyWithErrorHandler
)。
However when I call a method on the proxy object (defined in the Protocol definition), I get back this error:
但是,当我调用代理对象(在协议定义中定义)上的方法时,我得到了这个错误:
Failed to connect to launch agent: Error Domain=NSCocoaErrorDomain Code=4099 "Couldn't communicate with a helper application.
Essentially no matter what I do I'm not able to communicate with my helper app. I'm doing nothing fancy, just trying to make a simple call to the helper app to NSLog() something. But it doesn't work. Strangely I also don't see any output from inside:
基本上无论我做什么,我都无法与我的助手应用程序进行通信。我没有做任何花哨的事情,只是试图对 NSLog() 的帮助应用程序进行简单的调用。但它不起作用。奇怪的是,我也没有看到内部的任何输出:
- (BOOL)listener:(NSXPCListener *)listener shouldAcceptNewConnection:(NSXPCConnection *)newConnection
What could I possibly be doing wrong?
我可能做错了什么?
UPDATE: Apparently if I uncheck 'Enable App Sandboxing' on my main app, it works! So there's something wrong with enabling sandboxing after which it does not want to communicate with my helper app. Do I need more entitlements? I've tried them all under xcode!
更新:显然,如果我在主应用程序上取消选中“启用应用程序沙盒”,它就可以工作!因此,启用沙盒后它不想与我的帮助应用程序通信有问题。我需要更多的权利吗?我在 xcode 下都试过了!
回答by Samir
Your helper application is sandboxed. Therefore, it cannot register a mach service dynamically, although Xcode allows it for debug purpose.
您的助手应用程序已被沙盒化。因此,它不能动态注册 mach 服务,尽管 Xcode 允许它用于调试目的。
However, when you add your helper application to login items (using SMLoginItemSetEnabled() ), launchd will automatically register a mach service for you named with its bundle identifier.
但是,当您将帮助应用程序添加到登录项时(使用 SMLoginItemSetEnabled() ),launchd 将自动为您注册一个 mach 服务,该服务以其包标识符命名。
Now your main application is sandboxed. Therefore, random mach communication is not allowed. The only way to make it work was to add a temporary mach lookup entitlement.
现在您的主应用程序已被沙盒化。因此,不允许随机马赫通信。让它工作的唯一方法是添加一个临时的 mach 查找权限。
Since 10.7.4. Apple introduced the application-groups entitlements as a solution for this case, where an application needs to communicate with a helper app.
从 10.7.4 开始。Apple 引入了应用程序组权利作为这种情况的解决方案,在这种情况下,应用程序需要与辅助应用程序进行通信。
Both applications have to share the same application-groups entitlement. It can be any value, but Apple requires that this value must start with your Team-ID (e.g: Team-id.myApp). Then, your helper application bundle identifier must start with that same entitlement (e.g Team-id.myApp.myHelperApp). After that, your main application can freely communicate with your helper application using a XPC communication with the service named with the helper application bundle identifier (i.e. Team-id.myApp.myHelperApp). Also, the two applications will share access to a group container folder named with the application group entitlement (e.g. ~/Library/Group Containers/Team-id.myApp), that you have to create manually if you will need it.
两个应用程序必须共享相同的应用程序组权利。它可以是任何值,但 Apple 要求此值必须以您的 Team-ID 开头(例如:Team-id.myApp)。然后,您的助手应用程序包标识符必须以相同的权利开头(例如 Team-id.myApp.myHelperApp)。之后,您的主应用程序可以使用 XPC 通信与使用助手应用程序包标识符命名的服务(即 Team-id.myApp.myHelperApp)自由地与您的助手应用程序通信。此外,这两个应用程序将共享对以应用程序组权限命名的组容器文件夹(例如 ~/Library/Group Containers/Team-id.myApp)的访问权限,如果需要,您必须手动创建该文件夹。
回答by strange
Okay so I've learned the hard way - there are tons of issues with Sandboxing and XPC not to mention helper apps and sharing databases using the so called 'shared group directory' which neither gets created automatically (as the documentation incorrectly says) nor does NSURL
offer the method it claims to in the documentation.
好的,所以我已经学会了艰难的方式 - 沙盒和 XPC 存在大量问题,更不用说使用所谓的“共享组目录”的辅助应用程序和共享数据库,该目录既不会自动创建(如文档错误所述),也不会自动创建NSURL
提供它在文档中声称的方法。
Although the documentation says that in the Entitlements you can specify any string as the 'shared app identifier' in the format <TEAM_ID>.whatever
, apparently it will ONLY work if you use the format: <TEAM_ID>.com.yourcompany
虽然文档说在权利中你可以指定任何字符串作为格式中的“共享应用程序标识符”,但<TEAM_ID>.whatever
显然它只有在你使用以下格式时才有效:<TEAM_ID>.com.yourcompany
Anything else and it won't work. It'll compile, it'll archive, it'll run but it won't let you talk to your helper app. After spending around 30 hours I thought heck I'll try that one last change and apparently that was it! Filing a radar on the horribly written sandboxing documentation (which a lot on Apple's Developer forum are complaining about) next...
别的什么都行不通。它会编译,会存档,会运行,但不会让您与助手应用程序对话。花了大约 30 个小时后,我想我会尝试最后一次更改,显然就是这样!接下来对写得很糟糕的沙盒文档(Apple 的开发者论坛上很多人都在抱怨)进行调查……