如何在iOS应用程序之间进行通信?

时间:2015-01-23 15:47:33

标签: ios objective-c jailbreak iphone-privateapi tweak

在我们使用CFMessagePort之前,但现在它对iOS7及更高版本无效,是否有替换方法?我在越狱环境中挂钩CFMessagePort的构造函数时尝试UIApplication,但在大多数应用程序中,它不能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();
    }
}

1 个答案:

答案 0 :(得分:2)

使用 CFNotificationCenterGetDarwinNotifyCenter

尝试CFNotificationCenter
#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);
}
  

这将收听名为 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);
}