Add Callback Stuff

This commit is contained in:
Stossy11 2025-06-14 14:35:51 +10:00
parent 8259cc3dca
commit 0ef2c018fe
3 changed files with 41 additions and 0 deletions

View File

@ -9,6 +9,11 @@
extern "C" {
#endif
typedef void (^SwiftCallback)(NSString *result);
void RegisterCallback(NSString *identifier, SwiftCallback callback);
void TriggerCallback(const char *cIdentifier);
void showAlert(const char *title, const char *message, bool showCancel);
void showKeyboardAlert(const char *title, const char *message, const char *placeholder);

View File

@ -12,6 +12,8 @@
extern "C" {
#endif
typedef void (^SwiftCallback)(NSString *result);
static char *keyboardInput = NULL;
UIWindowScene *getMainDeviceWindowScene() {
@ -157,6 +159,40 @@ void clearKeyboardInput() {
}
}
static NSMutableDictionary<NSString*, SwiftCallback> *callbackStore;
void RegisterCallback(NSString *identifier, SwiftCallback callback) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
callbackStore = [NSMutableDictionary new];
});
if (identifier && callback) {
@synchronized(callbackStore) {
callbackStore[identifier] = [callback copy]; // copy blocks
}
}
}
void TriggerCallback(const char *cIdentifier) {
if (!cIdentifier) return;
// Convert C string to NSString
NSString *identifier = [NSString stringWithUTF8String:cIdentifier];
if (!identifier) return;
SwiftCallback callback = nil;
@synchronized(callbackStore) {
callback = callbackStore[identifier];
if (callback) {
[callbackStore removeObjectForKey:identifier]; // optional: remove after call
}
}
if (callback) {
callback(identifier);
}
}
#ifdef __cplusplus
}
#endif