本文介绍了如何在iOS中的应用程序之间进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们使用 CFMessagePort 之前,但现在它对于iOS7及以上版本无效,是否有任何替换方法?在越狱环境中挂钩 UIApplication 的构造函数时,我尝试了 CFMessagePort ,但在大多数应用程序中,它可以' t CFMessagePortCreateLocal 成功,它只返回NULL.Am我错了?

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();
    }
}


推荐答案

尝试使用 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);
}



发布通知

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中的应用程序之间进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-14 10:01