Tweak进程通信sendMessageAndReceiveReplyName问题

参考github上的代码,还有搜索引擎的结果,说进程通信 sendMessageAndReceiveReplyName 是会wait返回值的,但是我在使用的时候,在控制台用sendMessageAndReceiveReplyName给tweak发送消息,控制台根本没有等待返回的数据,百思不得解,请大牛们帮忙

tweak上的代码

-(void)applicationDidFinishLaunching:(id)applicationDidFinishLaunching
{
        %orig;
        CPDistributedMessagingCenter *center = [CPDistributedMessagingCenter centerNamed:@"com.mycompany.myCenter"];
        ;
        ;
}

%new(v@:@@)
- (NSDictionary *) handleMessageNamed:(NSString *)name userInfo :(NSDictionary *)userInfo 
{
        
        if([name isEqualToString: @"dialog"])
        {
                NSString *body = [userInfo objectForKey:@"keybody"];
                NSString *time = [userInfo objectForKey:@"keytime"];
                NSNumberFormatter *f = [NSNumberFormatter alloc] init];
                id result=[f numberFromString:time];
                [f release];
                if(!(result))  {  
                        result=time;  
                        } 
                
                UIAlertView *alertView=[UIAlertView alloc] initWithTitle:@"message" message:body delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
                [alertView show];
                if ([result floatValue] >0 )
                {
                         [self performSelector:@selector(dimissAlert:) withObject:alertView afterDelay:[result floatValue]];
                }
                [alertView release];
                
        }

        return @{ @"OK" : @"dialog" };
}

控制台发送消息代码

center = [CPDistributedMessagingCenter centerNamed:@"com.mycompany.myCenter"];
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:keybody,@"keybody", keytime,@"keytime", nil];
        NSDictionary *NSDictionary=;
        NSString *messageReply = [NSDictionary objectForKey:@"dialog"];
        const char * Reply =[messageReply UTF8String];
        printf("error is %s\n",[error localizedDescription]UTF8String]);

你的问题是什么?期待出现什么样的结果,实际出现什么样的结果?

我目的是在控制台上获取tweak上的一个函数返回值,举个简单例子就是在tweak上有个加法函数,我在控制台上通过进程通信调用这个加法的函数,然后获取这个结果,如果tweak上没有执行完这个函数,控制台就要一直等待,直到有结果返回

我意思是你的代码出现了什么问题?本来应该是什么执行效果,实际得到什么结果?

本来(或者说我认为)是要等待tweak的返回值的,但是现在没有等待tweak返回值,直接执行控制台sendMessageAndReceiveReplyName下面的代码了,我想实现的是发消息,然后等待返回消息,然后再执行后边代码,现在情况就是发送消息后没有等待返回就直接执行后边的代码了

NSDictionary *dictionary = ;

这一句执行后,dictionary有值吗?

没有值,实际上tweak上是要等待用户操作的,有个对话框要用户点掉才会返回然后控制台继续运行,而执行情况是tweak弹出对话框,等待用户点击,但是控制台已经执行结束了

- (NSDictionary *) handleMessageNamed:(NSString *)name userInfo :(NSDictionary *)userInfo 

你的这个函数里面,最后一行本来就是直接return了,哪来的等待?

不是handleMessageNamed函数等待,他是tweak上执行操作以后进行返回,我需要的是控制台上的sendMessageAndReceiveReplyName获取handleMessageNamed的返回值,现在情况就是控制台上的sendMessageAndReceiveReplyName没有等待到handleMessageNamed的返回值就继续执行下面的函数了

handleMessageNamed的返回值不就是@{ @“OK” : @“dialog” }么?还要等什么?

对,他的返回值就是这个,问题在于控制台那边要用sendMessageAndReceiveReplyName这个函数得到这个返回值,http://www.iphonedevwiki.net/index.php/CPDistributedMessagingCenter 这里说的这个函数要“ (wait for reply)”,可是我调用这个函数就没有wait到 handleMessageNamed的返回值

下面是百度的代码,Two-way (wait for reply),是要wait到返回的,我调用的时候就没有返回值就执行其他代码了

Client
CPDistributedMessagingCenter *messagingCenter;
messagingCenter = [CPDistributedMessagingCenter centerNamed:@"unique.name.for.messaging.center"];
 
// One-way (message only)
[messagingCenter sendMessageName:@"message" userInfo:nil/* optional dictionary. in this example it will be ignored. */];
 
// Two-way (wait for reply)
NSDictionary *reply;
reply = [messagingCenter sendMessageAndReceiveReplyName:@"messageThatHasInfo" userInfo:nil/* optional dictionary */];

也就是说,你执行

NSDictionary *dictionary = ;

之后,dictionary是个nil?

是的,直接就执行后边的代码了

你的tweak位于哪个进程?控制台的话,就是一个命令行程序是吧?

tweak在springboard控制台就是命令行程序,tweak是server

你确定

-(NSDictionary *) handleMessageNamed:(NSString *)name userInfo :(NSDictionary *)userInfo

得到执行了吗?

执行了,tweak的弹窗也弹出来了