2015-01-23 3 views
0

Прежде чем мы сможем использовать CFMessagePort, но теперь это недействительно для iOS7 и выше, есть ли какие-либо замененные методы? Я пробовал CFMessagePort при подключении конструктора UIApplication в среде джейлбрейка, но в большинстве приложений он не может успешно завершить CFMessagePortCreateLocal, он просто возвращает NULL.Am Я где-то ошибаюсь?Как общаться между приложениями в iOS?

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

Возможный дубликат [Передача и сохранение данных между приложениями с группами приложений] (http://stackoverflow.com/questions/24015506/communicating-and-persisting-data-between-apps-with-app-groups) – TCB13

+0

URL: https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Inter-AppCommunication/Inter-AppCommunication.html – TCB13

+0

Это вопрос с джейлбрейком, поэтому для него требуются API более низкого уровня. – creker

ответ

2

попробовать 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); 
} 

Это будет слушать уведомлений имени 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); 
}