开发者

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 :

        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)


This should work...

Add property to header file ( How To Add A Property In Objective C )

 -(NSString *) jsCallback;

Get the javascript callback method and set the property

- (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

- (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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜