当前位置:   article > 正文

MacOS XPC的使用入门_mac开发xpc service

mac开发xpc service

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声明一些函数,

  1. // XPCServiceProtocol.h文件
  2. #import <Foundation/Foundation.h>
  3. @protocol XPCServiceProtocol
  4. - (void)upperCaseString:(NSString *)aString withReply:(void (^)(NSString *))reply;
  5. @end

XPCService.h声明继承了协议,

  1. // XPCService.h文件
  2. #import <Foundation/Foundation.h>
  3. #import "XPCServiceProtocol.h"
  4. @interface XPCService : NSObject <XPCServiceProtocol>
  5. @end

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

  1. // XPCService.m文件
  2. #import "XPCService.h"
  3. @implementation XPCService
  4. // XPCServiceProtocol.h中声明的方法的实现
  5. - (void)upperCaseString:(NSString *)aString withReply:(void (^)(NSString *))reply {
  6. NSString *response = [aString uppercaseString];
  7. reply(response);
  8. }
  9. @end

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

 


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

  1. /*
  2. To use the service from an application or other process, use NSXPCConnection to establish a connection to the service by doing something like this:
  3. _connectionToService = [[NSXPCConnection alloc] initWithServiceName:@"com.company.XPCService"];
  4. _connectionToService.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(XPCServiceProtocol)];
  5. [_connectionToService resume];
  6. Once you have a connection to the service, you can use it like this:
  7. [[_connectionToService remoteObjectProxy] upperCaseString:@"hello" withReply:^(NSString *aString) {
  8. // We have received a response. Update our text field, but do it on the main thread.
  9. NSLog(@"Result string was: %@", aString);
  10. }];
  11. And, when you are finished with the service, clean up the connection like this:
  12. [_connectionToService invalidate];
  13. */

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

  1. // ViewController.h文件
  2. #import <Cocoa/Cocoa.h>
  3. #import "XPCServiceProtocol.h"
  4. #import "XPCService.h"
  5. @interface ViewController : NSViewController
  6. @end

ViewController.m文件内容:

  1. // ViewController.m文件
  2. #import "ViewController.h"
  3. @interface ViewController(){
  4. NSXPCConnection *_connectionToService; //私有变量
  5. }
  6. @end
  7. @implementation ViewController
  8. - (void)viewDidLoad {
  9. [super viewDidLoad];
  10. //建立xpc连接并启动
  11. _connectionToService = [[NSXPCConnection alloc] initWithServiceName:@"com.company.XPCService"];
  12. _connectionToService.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(XPCServiceProtocol)];
  13. [_connectionToService resume];
  14. }
  15. - (void)setRepresentedObject:(id)representedObject {
  16. [super setRepresentedObject:representedObject];
  17. // Update the view, if already loaded.
  18. }
  19. - (IBAction)XPCTest:(id)sender {
  20. // XPC中运行方法
  21. [[_connectionToService remoteObjectProxy] upperCaseString:@"hello" withReply:^(NSString *aString) {
  22. NSLog(@"Result string was: %@", aString);
  23. }];
  24. }
  25. @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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/236334
推荐阅读
相关标签
  

闽ICP备14008679号