简体   繁体   English

如何实现对JavaScript的phonegap回调

[英]How to implement a phonegap call back to javascript

I have the following code perfectly working, except ... well for the call back ! 我有以下代码可以正常工作,除了...很好的回调!

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

I then call the function from javascript : 然后,我从javascript调用函数:

        function getIt(){
            PhoneGap.exec("BarcodeReader.readBarcode", "myCallback");
        }

Problem is that I don't understand how to call the 'myCallBack' function from c# (admit i'm a total newbie) 问题是我不理解如何从C#调用“ myCallBack”函数(承认我是新手)

This should work... 这应该工作...

Add property to header file ( How To Add A Property In Objective C ) 将属性添加到头文件( 如何在目标C中添加属性

 -(NSString *) jsCallback;

Get the javascript callback method and set the property 获取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]; 
} 

Use the javascript callback method here 在此处使用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];
}

Please mark the other answer I provided you as correct also 把我提供给您的其他答案也标记为正确

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM