how to take a URL from a QR code via ZBarSDK and directly present it in UIWebView

1k views Asked by At

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.

1

There are 1 answers

4
Mrunal On

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 
}