MOLXPCConnection
围绕 NSXPCListener 和 NSXPCConnection 的一种包装,它可以提供客户端复用、连接客户端签名的验证、强制连接建立以及为特权和非特权客户端提供不同的导出接口。
安装
使用 CocoaPods
将以下行添加到 Podfile 中
pod 'MOLCodesignChecker'
使用 Bazel
将以下内容添加到您的 WORKSPACE 文件中
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
# Needed for MOLXPConnection
git_repository(
name = "MOLCertificate",
remote = "https://github.com/google/macops-molcertificate.git",
tag = "v2.0",
)
# Needed for MOLXPCConnection
git_repository(
name = "MOLCodesignChecker",
remote = "https://github.com/google/macops-molcodesignchecker.git",
tag = "v2.0",
)
git_repository(
name = "MOLXPCConnection",
remote = "https://github.com/google/macops-molxpcconnection.git",
tag = "v2.0",
)
并在您的 BUILD 文件中,将 MOLXPCConnection 添加为依赖项
objc_library( name = "MyAwesomeApp_lib", srcs = ["src/MyAwesomeApp.m", "src/MyAwesomeApp.h"], deps = ["@MOLXPCConnection//:MOLXPCConnection"], )
示例
由 launchd
启动的服务器示例,其中 launchd
任务具有 MachServices
键
MOLXPCConnection *conn = [[MOLXPCConnection alloc] initServerWithName:@"MyServer"];
conn.privilegedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(MyServerProtocol)];
conn.exportedObject = myObject;
[conn resume];
示例客户端,连接到上述服务器
MOLXPCConnection *conn = [[MOLXPCConnection alloc] initClientWithName:"MyServer" withOptions:0];
conn.remoteInterface = [NSXPCInterface interfaceWithProtocol:@protocol(MyServerProtocol)];
conn.invalidationHandler = ^{ NSLog(@"Connection invalidated") };
[conn resume];
客户端可以通过以下方式向服务器发送消息:
[conn.remoteObjectProxy selectorInRemoteInterface];
MOLXPCConnection的工作方式相较于直接使用NSXPCConnection有优势,即从客户端开始,一旦resume方法执行完毕,连接要么有效,要么调用无效处理程序。通常,连接直到第一条消息发送过来之前都不会真正建立。
消息始终在后台线程上传递!
文档
参考文档在CocoaDocs.org
http://cocoadocs.org/docsets/MOLXPCConnection
贡献
欢迎对这个库的补丁。请参阅CONTRIBUTING文件。