随笔 - 262  文章 - 0  评论 - 5  阅读 - 20万

【iOS 入门】extension

复制代码
这里没有介绍 todayextension用法,这个先不记了。
记录主要一个功能。extension如何与主app交互。
extension与app属两个独立的进程。 不可以直接能讯。适用IOS进程间通信方式。
CFNotificationCenterGetDarwinNotifyCenter 是其中一种。




CFNotificationCenterGetDarwinNotifyCenter!这是CoreFoundation库中一个系统级的通知中心,苹果的系统自己也在用它,看清了“Darwin””了没有?哈哈!看了下CFNotificationCenter相关的API,跟NSNotificationCenter有点像。需要用到Toll-Bridge的知识与CoreFoundation相关的类进行桥接,这虽不常用但也不难。还需要注意下个别参数的使用。 // 添加监听 - (void)addObserver { CFNotificationCenterRef notification = CFNotificationCenterGetDarwinNotifyCenter (); CFNotificationCenterAddObserver(notification, (__bridge const void *)(self), observerMethod, CFSTR(“通知名”), NULL, CFNotificationSuspensionBehaviorDeliverImmediately); } void observerMethod (CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) { //监听到notification后要做的处理 } // 移除监听 - (void)removeObserver { CFNotificationCenterRef notification = CFNotificationCenterGetDarwinNotifyCenter (); CFNotificationCenterRemoveObserver(notification, (__bridge const void *)(self), CFSTR(“通知名“), NULL); } // 发送通知 - (void)postNotificaiton { CFNotificationCenterRef notification = CFNotificationCenterGetDarwinNotifyCenter (); CFNotificationCenterPostNotification(notification, CFSTR(“通知名”), NULL, NULL, YES); } 使用这个方法后,容器App调用addObserver,扩展改变音量的时候调用 postNotificaiton ,容器App就能接收到通知了。 下一步是要传输数据,看看发送和接收的方法的参数, void CFNotificationCenterPostNotification ( CFNotificationCenterRef center, CFStringRef name, const void *object, CFDictionaryRef userInfo, Boolean deliverImmediately ); void observerMethod (CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) 还真有 CFDictionaryRef userInfo ,太好了,赶紧爽一下! CFNotificationCenterPostNotification(notification, CFSTR(“通知名”), NULL, 数据, YES); 容器App接收到的 userInfo 居然是nil!!百思不得骑姐其解,只能看文档了: CFNotificationCenterPostNotification 文档 妈蛋,传不过去! but!无所谓啦~ㄟ( ▔, ▔ )ㄏ,一开始不是已经解决了共享数据的问题了么,扩展发送通知前先存储数据,容器App接收到通知后,再读取共享数据那就好了~~~用这个思路,就能实现大部分的扩展与容器App之间的交互功能了~ BTW,如果想实现更复杂的功能,推荐使用MMWormhole这个开源库,它专门用于在Container app 与 Extension间传递消息,苹果婊 Watch OS 也适用~ 对了,如果用Jenkins打包,一定要注意widget 的app id (不是容器app 的app id)和app groups是正确的,否用 application loader提交程序的时候,会报错。 作者:十一点就要睡觉的码农 链接:https://www.jianshu.com/p/e807ee6e46e5 来源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 先记个地址https://www.cnblogs.com/junhuawang/p/8178276.htm
复制代码

 

posted on   wp7ers  阅读(318)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示