iOS进程间通信之CFMessagePort
本文转载至 http://www.cocoachina.com/industry/20140606/8701.html
iOS系统是出了名的封闭,每个应用的活动范围被严格地限制在各自的沙盒中。尽管如此,iOS还是提供了若干进程间通信机制,CFMessagePort就是其中之一。
- -(void)startListenning
- {
- if (0 != mMsgPortListenner && CFMessagePortIsValid(mMsgPortListenner))
- {
- CFMessagePortInvalidate(mMsgPortListenner);
- }
- mMsgPortListenner = CFMessagePortCreateLocal(kCFAllocatorDefault,CFSTR(LOCAL_MACH_PORT_NAME),onRecvMessageCallBack, NULL, NULL);
- CFRunLoopSourceRef source = CFMessagePortCreateRunLoopSource(kCFAllocatorDefault, mMsgPortListenner, 0);
- CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopCommonModes);
- NSLog(@"start listenning");
- }
- #define LOCAL_MACH_PORT_NAME "com.wangzz.demo"
- typedef CFDataRef (*CFMessagePortCallBack) (
- CFMessagePortRef local,
- SInt32 msgid,
- CFDataRef data,
- void *info
- );
- CFDataRef onRecvMessageCallBack(CFMessagePortRef local,SInt32 msgid,CFDataRef cfData, void*info)
- {
- NSLog(@"onRecvMessageCallBack is called");
- NSString *strData = nil;
- if (cfData)
- {
- const UInt8 * recvedMsg = CFDataGetBytePtr(cfData);
- strData = [NSString stringWithCString:(char *)recvedMsg encoding:NSUTF8StringEncoding];
- /**
- 实现数据解析操作
- **/
- NSLog(@"receive message:%@",strData);
- }
- //为了测试,生成返回数据
- NSString *returnString = [NSString stringWithFormat:@"i have receive:%@",strData];
- const char* cStr = [returnString UTF8String];
- NSUInteger ulen = [returnString lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
- CFDataRef sgReturn = CFDataCreate(NULL, (UInt8 *)cStr, ulen);
- return sgReturn;
- }
- - (void)endLisenning
- {
- CFMessagePortInvalidate(mMsgPortListenner);
- CFRelease(mMsgPortListenner);
- }
- -(NSString *)sendMessageToDameonWith:(id)msgInfo msgID:(NSInteger)msgid
- {
- // 生成Remote port
- CFMessagePortRef bRemote = CFMessagePortCreateRemote(kCFAllocatorDefault, CFSTR(MACH_PORT_REMOTE));
- if (nil == bRemote) {
- NSLog(@"bRemote create failed");
- return nil;
- }
- // 构建发送数据(string)
- NSString *msg = [NSString stringWithFormat:@"%@",msgInfo];
- NSLog(@"send msg is :%@",msg);
- const char *message = [msg UTF8String];
- CFDataRef data,recvData = nil;
- data = CFDataCreate(NULL, (UInt8 *)message, strlen(message));
- // 执行发送操作
- CFMessagePortSendRequest(bRemote, msgid, data, 0, 100 , kCFRunLoopDefaultMode, &recvData);
- if (nil == recvData) {
- NSLog(@"recvData date is nil.");
- CFRelease(data);
- CFMessagePortInvalidate(bRemote);
- CFRelease(bRemote);
- return nil;
- }
- // 解析返回数据
- const UInt8 * recvedMsg = CFDataGetBytePtr(recvData);
- if (nil == recvedMsg) {
- NSLog(@"receive date err.");
- CFRelease(data);
- CFMessagePortInvalidate(bRemote);
- CFRelease(bRemote);
- return nil;
- }
- NSString *strMsg = [NSString stringWithCString:(char *)recvedMsg encoding:NSUTF8StringEncoding];
- NSLog(@"%@",strMsg);
- CFRelease(data);
- CFMessagePortInvalidate(bRemote);
- CFRelease(bRemote);
- CFRelease(recvData);
- return strMsg;
- }
- #define MACH_PORT_REMOTE "com.wangzz.demo"
This method is not available on iOS 7 and later—it will return NULL and log a sandbox violation in syslog. See Concurrency Programming Guide for possible replacement technologies. |
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。