2

I need to embed a QR code reader to my app. So far, ZBarSDK seems to be a good solusion for ios. I have grabbed some examples and tutorials, and embedded the function to my app successfully.

However, For the tutorials I found are all just show the result with an alert.

In my case, the QR code I'm going to use will contain a url, what I need is to present the web directly after scanning. The web needs to be opened in UIweb browser which contains navigation bar so that users can easily go back to the app. I'm a beginner of app development. it would be grateful if anyone can provide some solutions for me. This is my code so far

- (IBAction)scanButtonPress:(id)sender
{
    ZBarReaderViewController *reader = [ZBarReaderViewController new];
    reader.readerDelegate = self;

    [reader.scanner setSymbology:ZBAR_UPCA config:ZBAR_CFG_ENABLE to:0];
    reader.readerView.zoom = 1.0;

    [self presentModalViewController:reader
                            animated:YES];
}

- (void)    imagePickerController:(UIImagePickerController *)reader didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    id <NSFastEnumeration> results = [info objectForKey:ZBarReaderControllerResults];

    ZBarSymbol *symbol = nil;

    for (symbol in results)
    {
        NSString *upcString = symbol.data;

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Scanned UPC" message:[NSString stringWithFormat:@"The UPC read was: %@", upcString] delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];

        [alert show];

        [reader dismissModalViewControllerAnimated:YES];
    }
}

I know the codes should be changed is there

 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Scanned UPC" message:[NSString stringWithFormat:@"The UPC read was: %@", upcString] delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];

    [alert show];

but I do not know what are the correct code for me to reach this function.

Richard
  • 125
  • 1
  • 9

1 Answers1

3

Hope this code snippet helps:

- (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info
{

  <NSFastEnumeration> results =
        [info objectForKey: ZBarReaderControllerResults];
    ZBarSymbol *symbol = nil;
    for(symbol in results)
        // EXAMPLE: just grab the first barcode
        break;

    // EXAMPLE: do something useful with the barcode data
    NSString *resultText = symbol.data;

    // ADD: dismiss the controller (NB dismiss from the *reader*!)
    [reader dismissModalViewControllerAnimated: YES];


    BOOL canOpenGivenURL = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:resultText]];

    if (canOpenGivenURL)
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:resultText]];
    else
        // Not valid URL -- show some alert 
}
Mrunal
  • 13,982
  • 6
  • 52
  • 96
  • strUrl comes from where? – D-eptdeveloper Sep 06 '13 at 10:23
  • @Mrunal Thanks for the valuable help. These codes work for me. However, I need to open the URL in UIweb browser which contains navigation bar rather than the safari so that users can easily go back to the app.Could you help me to change the code? – Richard Sep 09 '13 at 13:14
  • Create a custom UIView which contains UIWebview and navigation bar with back button. You require to add-remove this view as per your requirement. And use fetched URL in this UIWebview only. – Mrunal Sep 11 '13 at 11:50
  • @Mrunal Thank you for your explanation, i have created a custom UIView with UIWebview and navigation bar, but which codes I should change in the codes you provided? – Richard Sep 12 '13 at 14:23