它的实现不再通过对象间的直接连接,而是通过block实现一种服务端对客户端的connection,这两者之间的通信都是通过使用xpc_connection发送消息实现。XPC的出现是为了将程序分成不同的几个子程序,从而实现权限分隔,让你的程序更加安全。
1.创建一个工程
可以是App project Xcode -> File -> New -> Project... -> macOS -> Application -> App -> click Next。
填写Product Name(XPCDemo) -> 选择或者不选择Team账号 -> click Next -> click create, 创建一个工程
2.创建xpc service
- 选中project -> 显示targets栏 -> 点击"+"号,
- 选择macOS -> Framework -> 选择XPC Service -> click Next
- 填写Product Name -> 设置 Bundle Identifer -> click Finish完成创建
3.检查和设置
-
创建完成后,检查下TARGETS -> XPCDemo -> Build Phases中,有增加一项 Embed XPC Services.
Xcode 11.0是自动添加的,以前的版本好像还要自己添加才行
-
检查或设置xpc的bundle identifer,在调用xpc时使用
4.文件说明
XPC 创建后,在工程中会生成4个文件,
- XPCServiceProtocol.h声明一些函数,是内外使用方法的桥梁,调用XPC的时候,调用Protocol中声明的方法
- ZZXXPCService在这个类中实现Protocol的代理方法
- main()
第一步:Main()注册监听
int main(int argc, const char *argv[])
{
// Create the delegate for the service.
ServiceDelegate *delegate = [ServiceDelegate new];
// Set up the one NSXPCListener for this service. It will handle all incoming connections.
NSXPCListener *listener = [NSXPCListener serviceListener];
listener.delegate = delegate;
// Resuming the serviceListener starts this service. This method does not return.
[listener resume];
return 0;
}
第二步:ServiceDelegate类实现NSXPCListenerDelegate的代理,代理内会启动XPC
- (BOOL)listener:(NSXPCListener *)listener shouldAcceptNewConnection:(NSXPCConnection *)newConnection {
// This method is where the NSXPCListener configures, accepts, and resumes a new incoming NSXPCConnection.
// Configure the connection.
// First, set the interface that the exported object implements.
newConnection.exportedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(ZZXXPCServiceProtocol)];
// Next, set the object that the connection exports. All messages sent on the connection to this service will be sent to the exported object to handle. The connection retains the exported object.
ZZXXPCService *exportedObject = [ZZXXPCService new];
newConnection.exportedObject = exportedObject;
// Resuming the connection allows the system to deliver more incoming messages.
[newConnection resume];
// Returning YES from this method tells the system that you have accepted this connection. If you want to reject the connection for some reason, call -invalidate on the connection and return NO.
return YES;
}
5.示例
在XPCServiceProtocol.h中给出了如何使用xpc的一个示例代码
示例中给出了建立连接、运行方法、清除连接的方法
ViewController.h中引入头文件:
@interface ViewController(){
NSXPCConnection *_connectionToService; //私有变量
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//建立xpc连接并启动
_connectionToService = [[NSXPCConnection alloc] initWithServiceName:@"com.zzx.ZZXXPCService"];
_connectionToService.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(ZZXXPCServiceProtocol)];
[_connectionToService resume];
[self XPCTest];
}
- (void)XPCTest{
// XPC中运行方法
[[_connectionToService remoteObjectProxy] upperCaseString:@"hello" withReply:^(NSString *aString) {
NSLog(@"Result string was: %@", aString);
}];
}
- (void)setRepresentedObject:(id)representedObject {
[super setRepresentedObject:representedObject];
// Update the view, if already loaded.
}
@end