飘云 发表于 2014-9-21 14:47:35

Tweak和app交互方案【进程通信】

Core Foundation DEMO:
Tweak端:      

CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
                                        NULL,
                                        &NotificationReceivedCallback,
                                        CFSTR("com.chinapyg.fakecarrier-change"),
                                        NULL,
                                        CFNotificationSuspensionBehaviorCoalesce);

回调:
static void NotificationReceivedCallback(CFNotificationCenterRef center,
                                       void *observer, CFStringRef name,
                                       const void *object, CFDictionaryRef
                                       userInfo)
{
//....可以根据 name来判断是何种消息,下面的客户端传了NULL,所以无需判断了,在多种消息的时候需要用到
}


APP端:
1.一句代码即可
notify_post("com.chinapyg.fakecarrier-change");

2.复杂点的

CFStringRef observedObject =
            CFSTR("com.chinapyg.fakecarrier-change");
CFNotificationCenterRef center =
            CFNotificationCenterGetDistributedCenter();
CFNotificationCenterPostNotification(center, NULL,
            observedObject, NULL /* no dictionary */, TRUE);


///////////////////////////////////////////////////////////////////////////////////////////
华丽的分割线
///////////////////////////////////////////////////////////////////////////////////////////
Cocoa DEMO:

接收端(后台):

NSString *observedObject = @"com.chinapyg.notification";
// 处理单个计算机上不同的进程之间的通知
NSDistributedNotificationCenter *center =
            ;
[center addObserver: self
            selector: @selector(callbackWithNotification:)
            name: @"PiaoYun Notification"
            object: observedObject];

回调:
- (void)callbackWithNotification:(NSNotification *)myNotification;
{
      NSLog(@"Notification Received");
}


发送端(app):


NSString *observedObject = @"com.mycompany.notification";
NSDistributedNotificationCenter *center =
            ;
[center postNotificationName: @"PiaoYun Notification"
            object: observedObject
            userInfo: nil /* no dictionary */
            deliverImmediately: YES];



iOS上层接口:

// 处理单进程之间的通知
[ addObserver: self selector: @selector(callBack) name: @"back" object: nil];

// 回调
- (void)callBack
{      
      NSLog(@"Notification Received");
}

//发出通知      
[ postNotificationName:@"back" object:self];


官方使用说明见:https://developer.apple.com/library/mac/documentation/userexperience/Conceptual/PreferencePanes/Tasks/Communication.html




347735313 发表于 2015-2-8 16:31:55

不错看看 楼主幸苦了

rootmm 发表于 2015-4-3 22:02:20

darwin notify运用甚广,apple watch通知也可用这个。

Mr.Dong 发表于 2016-12-9 02:05:22

这个方法好不能传参数过去

overlords 发表于 2017-1-8 23:34:14

学习了! 感谢飘云大神
页: [1]
查看完整版本: Tweak和app交互方案【进程通信】