XPC 是 macOS 里經(jīng)常用到的進(jìn)程間通信機(jī)制眯杏。本文不關(guān)注 XPC 的內(nèi)部機(jī)制,而是一篇教你如何上手 XPC 的文章壳澳。
創(chuàng)建 macOS 工程 'FRTestXPC'岂贩,選擇 Cocoa App Template。
在工程 'FRTestXPC' 里點(diǎn)擊增加 Target巷波,在彈出框搜索 XPC, 選擇搜索結(jié)果中的 XPC Service萎津,然后命名 Target 為 'FRTestXPC_Helper' 并將 target 對(duì)應(yīng)的 info.plist 文件中 bundle id 修改為 'com.frank.xpchelper'
選擇 'FRTestXPC' Target -> Build Phases 然后新增 CopyFiles 項(xiàng)目,將剛剛的 FRTestXPC_Helper.xpc 加進(jìn)去抹镊,注意 Destination 必須要選擇 XPC Services锉屈。否則后續(xù)主程序和 XPC 會(huì)無法建立連接。
回過頭來看工程目錄里 FRTestXPC_Helper -> main.m 文件垮耳,這里 main 函數(shù)是 XPC 程序的入口函數(shù)颈渊。可以看到 Xcode 已經(jīng)幫助生成很多 demo code终佛。我們已經(jīng)不需要做太多俊嗽。
-
在主程序的窗口里增加一個(gè) button,在點(diǎn)擊方法里實(shí)現(xiàn)如下代碼
NSXPCConnection *con = [[NSXPCConnection alloc] initWithServiceName:@"com.frank.xpchelper"]; NSXPCInterface *remoteInterface = [NSXPCInterface interfaceWithProtocol:@protocol(FRTestXPC_HelperProtocol)]; con.remoteObjectInterface = remoteInterface; [[con remoteObjectProxy] upperCaseString:@"hello" withReply:^(NSString *reply) { NSLog(@"reply = %@",reply); }]; [con resume];
啟動(dòng)程序,點(diǎn)擊 button查蓉,觀察終端輸出為
HELLO
一些說明
- Tips :作為 demo 可以刪除掉 Project 里的各種 Test Target 和 Test 文件乌询,保持工程清爽直觀。
- 我用的是 Xcode9.1版本豌研,創(chuàng)建好的 XPC 中的代碼完全為 Xcode 自動(dòng)生成的妹田,
(void)upperCaseString:(NSString *)aString withReply:(void (^)(NSString *))reply;
這個(gè)協(xié)議也是 Xcode 生成的,所以看上面代碼不要太突兀鹃共,你自己試試就知道了鬼佣。 - 初始化主程序 NSXPCConnection 的時(shí)候必須寫正確 XPC 的 service name.
一些 QA
Q: 什么場(chǎng)景下會(huì)用到 XPC? 具體到項(xiàng)目中說一下。
A: 我覺得用到 XPC 的地方主要還是希望讓別的進(jìn)程分擔(dān)自己的任務(wù)霜浴,同時(shí)又能夠及時(shí)的和主進(jìn)程進(jìn)行通信晶衷。(比如一些網(wǎng)絡(luò)處理的服務(wù))
- #Offcial Note# Apple WWDC 的講解也是這么說的 So if you just have some background piece of work that needs to happen every so often, you can use XPC to talk to it, initiate it, and get it running.
? 其次是考慮一些其他因素,比如
- 增加程序穩(wěn)定性(XPC Service crash 但主程序不 crash).
- XPC 服務(wù)處理一些不夠安全的東西阴孟,這時(shí)候我們不提供 XPC 任何權(quán)限晌纫,比如網(wǎng)絡(luò)訪問,文件訪問等永丝,但是主程序還是保留所有的權(quán)限锹漱。
- #Fan's Note# 相當(dāng)于讓小弟干活兒,但是不給小弟太多權(quán)利慕嚷,干的了就干哥牍,干不了就別干…..
- 很重要一點(diǎn):是這是 App Store 允許的多進(jìn)程的方式毕泌。
Q: XPC 服務(wù)有界面嗎?
A: 一般都是后臺(tái)進(jìn)程。沒見過有人用 XPC 搭建界面的嗅辣。
Q: 系統(tǒng)是怎么通過以下這些 API 方法找到 XPC 服務(wù)的 撼泛?
NSXPCInterface *myCookieInterface = [NSXPCInterfaceinterfaceWithProtocol:@protocol(XPC_HelperProtocol)]; NSXPCConnection *myConnection = [[NSXPCConnection alloc] initWithServiceName:@"com.frank.xpchelper"];
A: 當(dāng) app 啟動(dòng)的時(shí)候,系統(tǒng)自動(dòng)注冊(cè) app 中可見的 XPC 服務(wù)澡谭,當(dāng) app 與某個(gè) XPC 服務(wù)建立連接愿题,然后向他發(fā)送包含事件服務(wù)的消息。
? When the app is launched, the system automatically registers each XPC service it finds into the namespace visible to the app. An app establishes a connection with one of its XPC services and sends it messages containing events that the service then handles
一個(gè) Demo
以上的 demo 是最簡(jiǎn)單的译暂,一般 XPC 用來做后臺(tái)下載處理比較合適抠忘,所以寫了一個(gè)比上面代碼復(fù)雜一點(diǎn)點(diǎn)(不過還是很簡(jiǎn)單)的 demo 放到 github 上,這個(gè) demo 是 XPC 雙向同步的外永。即 XPC Service 可以不依賴于代理方法的回調(diào)而是直接給 Client app 發(fā)送消息崎脉。
NSXPCConnection 是最主要的 API,其余的 API 比如 NSXPCInterface伯顶,都是依附于 Connection囚灼。構(gòu)建雙向通信的 XPC Demo 在 ClientApp 和 XPC 里有一些代碼非常相似。即互相暴露接口祭衩,互相暴露交接的對(duì)象灶体。Demo 也提供了一些非常實(shí)用的構(gòu)建 XPC 時(shí)用的 API。
Demo 地址: https://github.com/fanxiushan/Demo.XPCTestDemo
參考:
- Creating XPC Services
- Mac Technology Overview -> Kernel and Device Drivers Layer -> High-Level Features -> XPC Interprocess Communication and Services
- WWDC: