0

I have a small c library that needs to do some cleanup when the app will enter background. How can i do that in C/C++. In Objective-C and swift it seems there are ways to register callbacks.

iOS13 or later

UIScene.willDeactivateNotification

iOS12 or earlier

UIApplication.willResignActiveNotification

[1] https://stackoverflow.com/a/34745677/811335

[2] https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622997-applicationdidenterbackground

A. K.
  • 34,395
  • 15
  • 52
  • 89
  • 1
    https://developer.apple.com/documentation/corefoundation/cfnotificationcenter-rkv – Alexander Sep 05 '20 at 00:56
  • Why don't you just call from objective-c notification handler c/c++ cleanup function/method? I would not make some utility c/c++ library (which usually cross-platform) dependent on OS specific frameworks, like CoreFoundation. – Asperi Sep 05 '20 at 06:57
  • That is the current status, and because of this it is getting difficult to use the library cross-platform. Most of the cleanup logic can be used in other places if i can find a C-API to register notification handler. – A. K. Sep 05 '20 at 16:57

1 Answers1

1

Based on CFNotificationCenter example here https://stackoverflow.com/a/6969178/5329717

#include <CoreFoundation/CoreFoundation.h>
#include <UIKit/UIApplication.h>

void uiApplicationWillResignNotificationCallback (CFNotificationCenterRef center,
                           void * observer,
                           CFStringRef name,
                           const void * object,
                           CFDictionaryRef userInfo) {
    CFShow(CFSTR("Received uiApplicationWillResignNotification"));
}

void exampleHandling() {
    CFNotificationCenterRef center = CFNotificationCenterGetLocalCenter();
    // add an observer
    CFNotificationCenterAddObserver(center, NULL, uiApplicationWillResignNotificationCallback,
                                    (__bridge CFStringRef)UIApplicationWillResignActiveNotification, NULL,
                                    CFNotificationSuspensionBehaviorDeliverImmediately);
    
    //remove observer
    CFNotificationCenterRemoveObserver(center, uiApplicationWillResignNotificationCallback, (__bridge CFStringRef)UIApplicationWillResignActiveNotification, NULL);
}
Kamil.S
  • 5,205
  • 2
  • 22
  • 51