Search code examples
iphoneobjective-cbarcodebarcode-scanner

Zbar Scanner symbol.data comparison iPhone Scanner


Current Code Explanation: The iPhone application incorporates the ZBar SDK to scan a bar code. The first bar code is returned and I send symbol.data (the barcode info) to updateTotal. In updateTotal, I check to see if the bar code that was scanned is the one I am looking for (declared as matchBarcode.) In this case, if the bar code is in fact the one I am looking for, it displays an alert with the bar code scanned saying so. If not, it displays an alert saying it is not the correct bar code.

The Problem: No matter what bar code is scanned, it returns that is not equal to matchBarcode string.

My thoughts: I figured it was something to do with barcode (symbol.data) not being an NSString but in the ZBar SDK it states that it is. I've been working on this for a while and can't figure this out. I'm sure it's a stupid mistake. Please help.

Current code:

- (void) imagePickerController: (UIImagePickerController*) reader
 didFinishPickingMediaWithInfo: (NSDictionary*) info
{
    // ADD: get the decode results
    id<NSFastEnumeration> results =
    [info objectForKey: ZBarReaderControllerResults];
    ZBarSymbol *symbol = nil;
    for(symbol in results)
        // EXAMPLE: just grab the first barcode
        break;

    [self updateTotal:symbol.data];

    NSLog(@"%@", symbol.data);
    // ADD: dismiss the controller (NB dismiss from the *reader*!)
    [reader dismissModalViewControllerAnimated: YES];
}




    -(void)updateTotal:(NSString *)barcode
    {
        // Barcode I am looking for
        NSString *matchBarcode = @"FoundBarcode";
        // Barcode Comparison
        if (barcode == matchBarcode) {
            // Alert stating this IS the barcode you are looking for
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Correct Barcode" message:barcode delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
        } else {
            // Alert stating this is not the barcode you are looking for
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid Barcode" message:barcode delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
        }
    }

Solution

  • Use isEqualToString method of NSString.

    if( [barcode isEqualToString: matchBarcode]){}