我正在尝试从一个 macOS 应用程序向另一个应用程序传递一个字符串(尽管我的真正目标是传递一个数组)。接收方应用经过沙盒处理,而发送方应用未经过沙盒处理。
我已向这两个应用添加了一个应用组 (DEV_TEAM_ID.com.if.APP_NAME.UNIQUE_NAME
)。
在接收器应用程序中,我已经实现了创建端口和运行循环:
CFMessagePortRef localPort =
CFMessagePortCreateLocal(nil,CFSTR("DEV_TEAM_ID.com.if.APP_NAME.UNIQUE_NAME"),PortCallBack,nil,nil);
CFRunLoopSourceRef runLoopSource =
CFMessagePortCreateRunLoopSource(nil,localPort,0);
CFRunLoopAddSource(CFRunLoopgetcurrent(),runLoopSource,kCFRunLoopCommonmodes);
当然还有回调:
cfdataRef PortCallBack(CFMessagePortRef local,SInt32 msgid,cfdataRef data,void *info) {
char *message = "Thanks for saying hello!";
cfdataRef returnData = cfdataCreate(NULL,(const UInt8 *)message,strlen(message)+1);
printf("here is our received data: %s\n",cfdataGetBytePtr(data));
return returnData;
}
在发件人应用中,我实施了:
CFMessagePortRef remotePort = CFMessagePortCreateRemote(nil,CFSTR("DEV_TEAM_ID.com.if.APP_NAME.UNIQUE_NAME"));
SInt32 messageIdentifier = 1;
cfdataRef messageData = (__bridge cfdataRef)[@"hello,friend." dataUsingEncoding:NSUTF8StringEncoding];
SInt32 status = CFMessagePortSendRequest(remotePort,messageIdentifier,messageData,1000,NULL,NULL);
if (status == kCFMessagePortSuccess)
{
NSLog(@"success");
}
接收方 应用程序启动正常,但发送方 应用程序在此行崩溃:
SInt32 status = CFMessagePortSendRequest(remotePort,NULL);
编辑: remotePort
似乎是 null,并导致崩溃
出现以下错误:
线程 1:EXC_BAD_accESS(代码=1,地址=0x8)
CoreFoundation`CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER:
0x7fff20707fe0 : testq %rdi,%rdi
0x7fff20707fe3 : je 0x7fff20707fed ;
0x7fff20707fe5 :pushq %rbp
0x7fff20707fe6 : movq %rsp,%rbp
0x7fff20707fe9 : callq *0x10(%rdi)
-> 0x7fff20707fec :popq %rbp
0x7fff20707fed :retq
0x7fff20707fee :nop
0x7fff20707fef :nop
0x7fff20707ff0 : nop
0x7fff20707ff1 :nop
0x7fff20707ff2 : nop
0x7fff20707ff3 :nop
0x7fff20707ff4 :nop
0x7fff20707ff5 :nop
0x7fff20707ff6 :没有
我不确定为什么 remotePort
会有 null 值。我在两个应用程序中使用相同的端口名称。
我一直在尝试使用 Stackoverflow 上的代码示例和其他帖子来解决这个问题,但我不明白我在哪里搞砸了。我将不胜感激任何建议!谢谢!
我看过的帖子:
https://nshipster.com/inter-process-communication/
CFMessagePort and sandboxing
Theos inter-app communication using mach ports
https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_security_application-groups