MacOS XPC的使用入门

MacOS:10.14,Xcode:11.0


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 -> 筛选框中输入xpc -> 选择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声明一些函数,

// XPCServiceProtocol.h文件
#import <Foundation/Foundation.h>

@protocol XPCServiceProtocol
- (void)upperCaseString:(NSString *)aString withReply:(void (^)(NSString *))reply;    
@end

XPCService.h声明继承了协议,

// XPCService.h文件
#import <Foundation/Foundation.h>
#import "XPCServiceProtocol.h"
@interface XPCService : NSObject <XPCServiceProtocol>
@end

XPCService.m中实现这些协议方法

// XPCService.m文件
#import "XPCService.h"

@implementation XPCService
// XPCServiceProtocol.h中声明的方法的实现
- (void)upperCaseString:(NSString *)aString withReply:(void (^)(NSString *))reply {
    NSString *response = [aString uppercaseString];
    reply(response);
}

@end

这个函数是xcode自动生成的示例代码,可直接用来测试。在main.m文件中也有很多代码,都是自动生成的,暂且不管
调用XPC的时候,调用Protocol中声明的方法,Protocol是内外使用方法的桥梁

 


5、XPC启动和方法调用
在XPCServiceProtocol.h中也给出了如何使用xpc的一个示例代码
示例中给出了建立连接、运行方法、清除连接的方法

/*
 To use the service from an application or other process, use NSXPCConnection to establish a connection to the service by doing something like this:

     _connectionToService = [[NSXPCConnection alloc] initWithServiceName:@"com.company.XPCService"];
     _connectionToService.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(XPCServiceProtocol)];
     [_connectionToService resume];

Once you have a connection to the service, you can use it like this:

     [[_connectionToService remoteObjectProxy] upperCaseString:@"hello" withReply:^(NSString *aString) {
         // We have received a response. Update our text field, but do it on the main thread.
         NSLog(@"Result string was: %@", aString);
     }];

 And, when you are finished with the service, clean up the connection like this:

     [_connectionToService invalidate];
*/

storyboard -> View Controller Scene -> 添加button -> ctrl+鼠标,将button在ViewController中建立个action连接
ViewController.h中引入头文件:

// ViewController.h文件
#import <Cocoa/Cocoa.h>
#import "XPCServiceProtocol.h"
#import "XPCService.h"
@interface ViewController : NSViewController

@end

ViewController.m文件内容:

// ViewController.m文件
#import "ViewController.h"
@interface ViewController(){
    NSXPCConnection *_connectionToService; //私有变量
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //建立xpc连接并启动
    _connectionToService = [[NSXPCConnection alloc] initWithServiceName:@"com.company.XPCService"];
    _connectionToService.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(XPCServiceProtocol)];
    [_connectionToService resume];
}

- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];

    // Update the view, if already loaded.
}
- (IBAction)XPCTest:(id)sender {
    // XPC中运行方法
    [[_connectionToService remoteObjectProxy] upperCaseString:@"hello" withReply:^(NSString *aString) {
        NSLog(@"Result string was: %@", aString);
    }];
}
@end

执行结果(XPC执行方法upperCaseString,输出结果):

 
 


github XPCDemo:https://github.com/auspark/XPCDemo


Apple Documentation:

Apple XPC
Creating XPC Services
Efficient Design with XPC Videos
Kernel and Device Drivers Layer

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章