0

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();
    }
}
Suge
  • 2,808
  • 3
  • 48
  • 79
  • possible duplicate of [Communicating and persisting data between apps with App Groups](http://stackoverflow.com/questions/24015506/communicating-and-persisting-data-between-apps-with-app-groups) – TCB13 Jan 23 '15 at 15:53
  • URLs: https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Inter-AppCommunication/Inter-AppCommunication.html – TCB13 Jan 23 '15 at 15:53
  • It's a jailbreak question thus requires lower level APIs. – creker Jan 23 '15 at 16:35
  • @Suge, what makes you think `CFMessagePort` is invalid? I use it even on iOS 8. Give us a bit more info where are you trying to use it and where it doesn't work any more. – creker Jan 23 '15 at 16:35
  • @creker, thank you, indeed I use `CFMessagePort` in the jailbreak environment.But in most of the apps, it can't `CFMessagePortCreateLocal` a `CFMessagePortRef` when I hook the constructor of `UIApplication`, it just return NULL.I don't what's the matter, can you help?I've update my code to the question.Thank you! – Suge Jan 24 '15 at 01:43
  • I presume you need a special entitlement, that only some apps have... sounds like it – Orph Jan 25 '15 at 09:04
  • It's probably due to sandbox restrictions in those apps - it's farly common that `CFMessagePort` is blocked in those. If it's a system app you can probably resign with sandbox profile removed from it's entitlements. I have an answer here explaining on how you can do that. – creker Jan 25 '15 at 19:22

1 Answers1

2

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);
}
Karim H
  • 1,543
  • 10
  • 24