在这篇关于Xcode源代码编辑器扩展的文章中,它提到了XPC是绕过应用程序沙箱的一种方法:
--只有Xcode才能加载扩展,才能加载扩展,而对SourceKit的调用则需要非沙箱化,这当然不会在App中运行。我们可以独立发布,并使用嵌入在扩展中的非沙箱XPC业务。
但是,我不知道如何将所有内容绑定在一起才能使用XPC服务。
如何将Xcode源代码编辑器扩展绑定到XPC服务?
发布于 2021-05-11 17:18:51
多亏了LinuxSupportForXcode扩展,我才能弄明白这一点。
我将假设您遵循关于创建Xcode扩展编辑器的教程,并使主要项目成为macOS应用程序。您应该具有与以下类似的目标结构:
要使用具有源编辑器扩展的XPC:
MyAppXPCService,其包标识符为com.example.MyAppXPCService。1. Go to your app's target.
2. Remove MyAppXPCService.xpc from the Frameworks and Libraries.
3. Go to your extension's target.
4. Add MyAppXPCService.xpc to Frameworks and Libraries by dragging it in from the products folder in the Project Navigator. Leave it on the default "Embed Without Signing".1. Create `main.swift`, `MyService.swift`, `MyServiceDelegate.swift`, `MyServiceProtocol.swift` normally.2. Set the following build settings: - **Install Objective-C Compatibility Header**: `NO`
- **Objective-C Generated Interface Header Name**: `` (blank)1. Choose your desired **Swift Language Version** in Build Settings.2. In Build Settings, _add_ (don't replace): `@loader_path/../../../../Frameworks` to **Runtime Search Paths**.如果您意外地替换,并使用嵌入式框架,XPC将在发射时坠毁。
1. `import MyAppXPCService` so that it can see the protocol.2. Create the connection, **using your XPC target's bundle identifier for** **`serviceName`**:私有let连接={ () -> NSXPCConnection in let connection = NSXPCConnection(serviceName:<#"com.example.MyAppXPCService"#>) connection.remoteObjectInterface = NSXPCInterface(with: MyAppXPCServiceProtocol.self)返回连接}()
3. Call your XPC service:私有函数xpcUpperCase(输入:字符串){ connection.resume()延迟{ connection.suspend() }保护,让xpcService = connection.remoteObjectProxy作为?MyAppXPCServiceProtocol output {引发NSError() }xpcService.upperCaseString(输入){输出在打印(输出)}
创建连接的好处是它将自动启动您的XPC服务,而不需要运行UI应用程序。
https://stackoverflow.com/questions/67491521
复制相似问题