美文网首页ios进阶酷
iOS进程间通信,CFNotificationCenterRef

iOS进程间通信,CFNotificationCenterRef

作者: codingchou | 来源:发表于2019-11-12 16:54 被阅读0次

CFNotificationCenterRefiOS 进程间通信的一种方式,是一种通知的机制,适用于container appextension app进行通信。
使用之前,需要为container appextension app设置 App Group,这样才能接收到彼此发送的进程间通知。
进程间通信如果需要传递复杂的数据,可以使用第三方库MMWormhole,网络上相关的教程非常多

  • 简单使用:
CFStringRef name = CFSTR("customName");
CFNotificationCenterRef center = CFNotificationCenterGetDarwinNotifyCenter();
//添加观察者
CFNotificationCenterAddObserver(center,
                                    (const void *)self,
                                    customNotification,
                                    name,
                                    NULL,
                                    kCFNotificationDeliverImmediately);
//发送通知
CFNotificationCenterPostNotification(center,
                                         name,
                                         (const void *)self,
                                         NULL,
                                         kCFNotificationDeliverImmediately);
  • 监听iOS 锁屏事件
//com.apple.springboard.lockstate
//之所以这样写是因为苹果明确不让开发者监听设备锁屏的情况,代码静态审核会不通过
    NSString *com = @"com";
    NSString *apple = @"apple";
    NSString *spring = @"spring";
    NSString *board = @"board";
    NSString *lock = @"lock";
    NSString *state = @"complete";
    self.lockCenterName = [NSString  stringWithFormat:@"%@.%@.%@%@.%@%@",com,apple,spring,board,lock,state];
    CFStringRef strRef = (__bridge CFStringRef)self.lockCenterName;
    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
                                    NULL,
                                    NULL,
                                    strRef,
                                    NULL,
                                    CFNotificationSuspensionBehaviorDeliverImmediately);
发送通知时的选项
typedef CF_ENUM(CFIndex, CFNotificationSuspensionBehavior) {
    //当进程/应用程序在后台时,服务器不会用这个名称和对象对任何通知进行排队。
    CFNotificationSuspensionBehaviorDrop = 1,
    //服务器只对指定名称和对象的最后一个通知进行排队;较早的通知将被删除。
    CFNotificationSuspensionBehaviorCoalesce = 2,
    //服务器将保存所有匹配的通知,直到队列被填满(队列大小由服务器决定),此时服务器可能刷新排队的通知。   
    CFNotificationSuspensionBehaviorHold = 3,
    //无论进程是否在后台,服务器都会发送与此注册匹配的通知。当匹配具有此暂停行为的通知时,它的效果是首先刷新任何排队的通知。
    CFNotificationSuspensionBehaviorDeliverImmediately = 4
};
  • 在工程中添加extension
    图1
  • 设置 App Groups,格式如下:
group.com.公司名.项目名
举例:
group.com.apple.test
图2

相关文章

  • iOS进程间通信,CFNotificationCenterRef

    CFNotificationCenterRef是iOS 进程间通信的一种方式,是一种通知的机制,适用于contai...

  • iOS进程间通信

    1.URL Schema 分享到第三方平台,或者是跳转到第三方平台支付等场景使用的是URL Schema。比如说从...

  • iOS 进程间通信

    进程间通信即应用间通信,典型的例子就是分享的时候打开微信,QQ等app,然后分享出去我们要分享的内容,这到底...

  • iOS进程间通信

    线程间通信 :通过performSelector系列的方法 可以实现 各种线程间的通信(通信 :调用与传参)进程间...

  • iOS 进程间通信(APP间通信)

    进程是指在系统中正在运行的一个应用程序,每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内,iOS ...

  • linux进程间通信(1)

    一、进程通信概述 1、什么是进程间通信?什么是线程间通信? 进程间通信: 进程间通信就指的是用户空间中进程A与进程...

  • 进程和线程

    关系 通信 iOS进程间通讯的心得 - LittleApple - 博客园

  • 第二十三章 进程间通信介绍(一)

    本章目标: 进程同步与进程互斥 进程间通信目的 进程间通信发展 进程间通信分类 进程间共享信息的三种方式 IPC对...

  • 进程间通信 (OSX/iOS)

    总起 OS X是MacOS与NeXTSTEP的结合。OC是Smalltalk类面向对象编程与C的结合。iCloud...

  • iOS 进程间通信翻译

    前言 原文的作者是 AFNetworking 的作者 Mattt Thompson 大神,原文链接。 正文 由于历...

网友评论

    本文标题:iOS进程间通信,CFNotificationCenterRef

    本文链接:https://www.haomeiwen.com/subject/wztnqctx.html