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];
復制代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM