我有一个小的c库,当应用程序进入后台时,它需要进行一些清理。我怎么能在C/C++中做到这一点呢。在Objective-C和swift中,似乎有注册回调的方法。
iOS13或更高版本
UIScene.willDeactivateNotification
iOS12或更早版本
UIApplication.willResignActiveNotification
[1]https://stackoverflow.com/a/34745677/811335
[2]https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622997-applicationdidenterbackground
基于此处的CFNotificationCenter
示例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);
}