如何实现对javascript的phonegap回调

如何实现对javascript的phonegap回调,javascript,cordova,Javascript,Cordova,我有下面的代码完美地工作,除了。。。好的,回电话 - (void)readBarcode:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options { ZBarReaderViewController *reader = [ZBarReaderViewController new]; reader.readerDelegate = self; ZBarImageScanner *scanne

我有下面的代码完美地工作,除了。。。好的,回电话

- (void)readBarcode:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options 
{ 
    ZBarReaderViewController *reader = [ZBarReaderViewController new]; 
    reader.readerDelegate = self;

    ZBarImageScanner *scanner = reader.scanner;
    [scanner setSymbology: ZBAR_EAN13
                   config: ZBAR_CFG_ENABLE
                       to: 1];

    [[super appViewController] presentModalViewController:reader animated:YES]; 
    [reader release]; 
} 

 (void) imagePickerController:(UIImagePickerController*)reader didFinishPickingMediaWithInfo: (NSDictionary*) info
{
    id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];

    ZBarSymbol *symbol = nil;
    for(symbol in results)
        break;

    resultText.text = symbol.data;
    resultImage.image = [info objectForKey: UIImagePickerControllerOriginalImage];

    NSString* retStr = [[NSString alloc] 
                        initWithFormat:@"%@({ code: '%@', image: '%@' });", 
                        resultText.text,resultImage.image];  

    [ webView stringByEvaluatingJavaScriptFromString:retStr ];  

    [reader dismissModalViewControllerAnimated: YES];
}
问题是我不明白如何从c#调用“myCallBack”函数(承认我完全是新手)

这应该可以

将属性添加到头文件()

获取javascript回调方法并设置属性

- (void)readBarcode:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options 
{ 
    ZBarReaderViewController *reader = [ZBarReaderViewController new]; 
    reader.readerDelegate = self;

    // New Property added !!!!
    NSString * jsCallback = [info objectAtIndex:0];

    ZBarImageScanner *scanner = reader.scanner;
    [scanner setSymbology: ZBAR_EAN13
                   config: ZBAR_CFG_ENABLE
                       to: 1];

    [[super appViewController] presentModalViewController:reader animated:YES]; 
    [reader release]; 
} 
在这里使用javascript回调方法

- (void) imagePickerController:(UIImagePickerController*)reader didFinishPickingMediaWithInfo: (NSDictionary*) info
{
    id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];

    ZBarSymbol *symbol = nil;
    for(symbol in results)
        break;

    resultText.text = symbol.data;
    resultImage.image = [info objectForKey: UIImagePickerControllerOriginalImage];

    // create the string
    NSString* retStr = [[NSString alloc] 
    initWithFormat:@"%@({ code: '%@', image: '%@' });", 
                            jsCallback,resultText.text,resultImage.image];  

    //execute
    [ webView stringByEvaluatingJavaScriptFromString:retStr ]; 

    [reader dismissModalViewControllerAnimated: YES];
}
-(void)imagePickerController:(UIImagePickerController*)读卡器已使用信息完成PickingMediaWithInfo:(NSDictionary*)信息
{
id结果=[info objectForKey:ZBarReaderControllerResults];
ZBarSymbol*符号=nil;
用于(结果中的符号)
打破
resultText.text=symbol.data;
resultImage.image=[info-objectForKey:UIImagePickerControllerOriginalImage];
//创建字符串
NSString*retStr=[[NSString alloc]
initWithFormat:@“%@({code:'%@',image:'%@});”,
jsCallback、resultText.text、resultImage.image];
//执行
[webView stringByEvaluatingJavaScriptFromString:retStr];
[reader dismissModalViewControllerAnimated:是];
}

请标记为正确

您没有接受我认为与此相同的上一个答案?同意,但问题不完整。谢谢aaron的回复;我已经把返回字符串放在了“imagePickerController”中,这就是为什么它不起作用,但是现在我得到了在imagepicker启动时实际调用的回调;我希望在图像被捕获后调用它。启动捕获2。获取回调。在示例中,回调出现在捕获结果之前。将代码移回方法imagePickerController在选择器被解除后,现在我得到一个“arguments not defined”错误。你能粘贴一个完整的代码吗?代码已经添加,你需要为javascript回调创建一个属性。。。然后在imagePickerController中使用该属性。
- (void)readBarcode:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options 
{ 
    ZBarReaderViewController *reader = [ZBarReaderViewController new]; 
    reader.readerDelegate = self;

    // New Property added !!!!
    NSString * jsCallback = [info objectAtIndex:0];

    ZBarImageScanner *scanner = reader.scanner;
    [scanner setSymbology: ZBAR_EAN13
                   config: ZBAR_CFG_ENABLE
                       to: 1];

    [[super appViewController] presentModalViewController:reader animated:YES]; 
    [reader release]; 
} 
- (void) imagePickerController:(UIImagePickerController*)reader didFinishPickingMediaWithInfo: (NSDictionary*) info
{
    id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];

    ZBarSymbol *symbol = nil;
    for(symbol in results)
        break;

    resultText.text = symbol.data;
    resultImage.image = [info objectForKey: UIImagePickerControllerOriginalImage];

    // create the string
    NSString* retStr = [[NSString alloc] 
    initWithFormat:@"%@({ code: '%@', image: '%@' });", 
                            jsCallback,resultText.text,resultImage.image];  

    //execute
    [ webView stringByEvaluatingJavaScriptFromString:retStr ]; 

    [reader dismissModalViewControllerAnimated: YES];
}