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ì)無(wú)法建立連接。
回過(guò)頭來(lái)看工程目錄里 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
一些說(shuō)明
- 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í)候必須寫(xiě)正確 XPC 的 service name.
一些 QA
Q: 什么場(chǎng)景下會(huì)用到 XPC? 具體到項(xiàng)目中說(shuō)一下。
A: 我覺(jué)得用到 XPC 的地方主要還是希望讓別的進(jìn)程分擔(dān)自己的任務(wù),同時(shí)又能夠及時(shí)的和主進(jìn)程進(jìn)行通信。(比如一些網(wǎng)絡(luò)處理的服務(wù))
- #Offcial Note# Apple WWDC 的講解也是這么說(shuō)的 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ò)訪問(wèn),文件訪問(wèn)等,但是主程序還是保留所有的權(quán)限。
- #Fan's Note# 相當(dāng)于讓小弟干活兒,但是不給小弟太多權(quán)利,干的了就干,干不了就別干…..
- 很重要一點(diǎn):是這是 App Store 允許的多進(jìn)程的方式。
Q: XPC 服務(wù)有界面嗎?
A: 一般都是后臺(tái)進(jìn)程。沒(méi)見(jiàn)過(guò)有人用 XPC 搭建界面的。
Q: 系統(tǒng)是怎么通過(guò)以下這些 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 中可見(jiàn)的 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 用來(lái)做后臺(tái)下載處理比較合適,所以寫(xiě)了一個(gè)比上面代碼復(fù)雜一點(diǎn)點(diǎn)(不過(guò)還是很簡(jiǎn)單)的 demo 放到 github 上,這個(gè) demo 是 XPC 雙向同步的。即 XPC Service 可以不依賴(lài)于代理方法的回調(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: