The main application and Extension are each a process, and the common communication mode is passive query, such as sharing of UserDefaults. If active notification is required, interprocess notification is required.
Send notification:
CFNotificationCenterRef notification = CFNotificationCenterGetDarwinNotifyCenter (); CFNotificationCenterPostNotification (notification, CFSTR (name "notice"), NULL, NULL, YES).Copy the code
Receive notifications
CFNotificationCenterRef notification = CFNotificationCenterGetDarwinNotifyCenter (); CFNotificationCenterAddObserver (notification, (__bridge const void *) (the observer), observerFunction and CFSTR (name "notice"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);Copy the code
ObserverFunction indicates the name of the notification response function in C format. The parameter format is as follows
void observerFunction (CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef the userInfo) {NSLog (@ "-- -- -- -- -- -- -- -- -- -- -- -- -- -- function response -- -- -- -- -- -- -- --"); }Copy the code