2010-11-24 108 views
0

我有以下代碼完美的工作,除了...以及回撥!如何實現回撥javascript的phonegap回撥

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

然後我從JavaScript調用該函數:

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

問題是,我不知道如何調用從C#中的「myCallBack函數」功能(承認我是一個總的新手)

+0

你沒有接受以前的答案,我相信這是一樣的嗎? http://stackoverflow.com/questions/4188581/how-to-implement-a-callback-with-phonegap – 2010-11-24 21:03:24

+0

同意了,但問題並不完整。 – Disco 2010-11-24 21:38:56

回答

1

這應該工作...

添加屬性,以頭文件(How To Add A Property In Objective C

-(NSString *) jsCallback; 

獲取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]; 
} 

請註明the other answer I provided you是正確的也