gpt4 book ai didi

ios - iOS应用程序之间如何通信?

转载 作者:行者123 更新时间:2023-11-28 21:51:33 31 4
gpt4 key购买 nike

之前我们可以使用CFMessagePort,现在iOS7及以上版本无效,请问有什么替代方法吗?我在越狱环境下hook UIApplication 的构造函数时尝试了CFMessagePort,但是在大多数应用中,它不能CFMessagePortCreateLocal成功,它只需返回 NULL。我错了吗?

static void setupUIApplicationMessagePort()
{
NSString *identifier = @"com.foo.foo.UIApplication";
CFMessagePortRef local = CFMessagePortCreateLocal(NULL, (__bridge CFStringRef)identifier, callBackForUIApplication, NULL, NULL);
if (local) {
NSLog(@"local OK: %@", local);

CFRunLoopSourceRef source = CFMessagePortCreateRunLoopSource(NULL, local, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopCommonModes);
rocketbootstrap_cfmessageportexposelocal(local);
} else {
NSLog(@"local is NULL"); // in most of the apps it returns NULL
}
}

%ctor {
if(%c(UIApplication)) {
setupUIApplicationMessagePort();
}
}

最佳答案

尝试 CFNotificationCenter使用 CFNotificationCenterGetDarwinNotifyCenter

#include <CoreFoundation/CFNotificationCenter.h>

/* This function will be called whatever a notification posted with the specified name */
void NotificationCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo){

}

void addObserver(){
CFStringRef name = CFSTR("NotificationName");
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),&NotificationCallback,name,NULL,CFNotificationSuspensionBehaviorDeliverImmediately);
}

This will listen to notifications named NotificationName

发布通知

void postNotification(){
CFStringRef name = CFSTR("NotificationName");
/* You have to create the userInfo dictionary and add all the keys to it */
CFDictionaryRef userInfo;
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), name, NULL, userInfo, true);
}

关于ios - iOS应用程序之间如何通信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28113564/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com