0

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)

Disco
  • 4,226
  • 11
  • 58
  • 76

1 Answers1

1

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

Community
  • 1
  • 1
Aaron Saunders
  • 33,180
  • 5
  • 60
  • 80
  • Thanks aaron for the reply; i had put the return string in the 'imagePickerController' that's why it didn't work but now I get the callback called actually on the imagepicker when it's launched; i want it to be called after the image is captured. – Disco Nov 24 '10 at 21:38
  • so i want : 1. Launch the capture 2. get the callback. With your example callback comes before capture result. – Disco Nov 24 '10 at 21:39
  • move the code back to the method imagePickerController after the picker is dismissed – Aaron Saunders Nov 24 '10 at 21:43
  • done, now i get a 'arguments not defined' error. Can you please paste a complete code ? – Disco Nov 24 '10 at 21:45
  • code has been added you need to create a property for the javascript callback... then use that property in the imagePickerController. – Aaron Saunders Nov 24 '10 at 22:00
  • Perfect ! Did the job ! Thank you very much. (you made a typo 'info' should be 'arguments'). – Disco Nov 24 '10 at 22:10
  • Quick update "myCallback" is never called back, any idea on that ? – Disco Nov 27 '10 at 11:38