Tweak和app互動方案【進程通訊】

來源:互聯網
上載者:User

標籤:

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];
複製代碼

Tweak和app互動方案【進程通訊】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.