How to communicate between applications in iOS?

2019-04-17 01:52发布

Before we can use CFMessagePort, but now it's invalid for iOS7 and above, is there any replaced methods? I tried CFMessagePort when hooking the constructor of UIApplication in the jailbreak environment, but in most of the apps, it can't CFMessagePortCreateLocal successfully, it just return NULL.Am I wrong somewhere?

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条回答
放我归山
2楼-- · 2019-04-17 02:25

try CFNotificationCenter using 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

To post a notification

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);
}
查看更多
登录 后发表回答