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

时间:2023-03-09 03:07:00
Tweak和app交互方案【进程通信】
Core Foundation DEMO:
Tweak端:
  1. CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
  2. NULL,
  3. &NotificationReceivedCallback,
  4. CFSTR("com.chinapyg.fakecarrier-change"),
  5. NULL,
  6. CFNotificationSuspensionBehaviorCoalesce);
  7. 回调:
  8. static void NotificationReceivedCallback(CFNotificationCenterRef center,
  9. void *observer, CFStringRef name,
  10. const void *object, CFDictionaryRef
  11. userInfo)
  12. {
  13. //....  可以根据 name来判断是何种消息,下面的客户端传了NULL,所以无需判断了,在多种消息的时候需要用到
  14. }

复制代码

APP端:
1.一句代码即可

  1. notify_post("com.chinapyg.fakecarrier-change");

复制代码

2.复杂点的

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

复制代码

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

接收端(后台):

  1. NSString *observedObject = @"com.chinapyg.notification";
  2. // 处理单个计算机上不同的进程之间的通知
  3. NSDistributedNotificationCenter *center =
  4. [NSDistributedNotificationCenter defaultCenter];
  5. [center addObserver: self
  6. selector: @selector(callbackWithNotification:)
  7. name: @"PiaoYun Notification"
  8. object: observedObject];
  9. 回调:
  10. - (void)callbackWithNotification:(NSNotification *)myNotification;
  11. {
  12. NSLog(@"Notification Received");
  13. }

复制代码

发送端(app):

  1. NSString *observedObject = @"com.mycompany.notification";
  2. NSDistributedNotificationCenter *center =
  3. [NSDistributedNotificationCenter defaultCenter];
  4. [center postNotificationName: @"PiaoYun Notification"
  5. object: observedObject
  6. userInfo: nil /* no dictionary */
  7. deliverImmediately: YES];

复制代码

iOS上层接口:

  1. // 处理单进程之间的通知
  2. [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(callBack) name: @"back" object: nil];
  3. // 回调
  4. - (void)callBack
  5. {
  6. NSLog(@"Notification Received");
  7. }
  8. //发出通知
  9. [[NSNotificationCenter defaultCenter] postNotificationName:@"back" object:self];

复制代码