如何通过ZBarSDK从QR代码中获取URL并直接在UIWebView中显示它

时间:2013-09-06 10:03:56

标签: iphone ios zbar-sdk

我需要在我的应用中嵌入QR码阅读器。到目前为止,ZBarSDK似乎对ios来说是一个很好的解决方案。我已经抓住了一些示例和教程,并将该函数成功嵌入到我的应用程序中。

但是,对于我发现的教程都只是通过警报显示结果。

就我而言,我要使用的二维码包含一个网址,我需要的是在扫描后直接呈现网页。需要在包含导航栏的UIweb浏览器中打开Web,以便用户可以轻松返回到应用程序。 我是应用程序开发的初学者。如果有人能为我提供一些解决方案,我将不胜感激。到目前为止,这是我的代码

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

我知道应该更改代码

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

    [alert show];

但我不知道达到此功能的正确代码是什么。

1 个答案:

答案 0 :(得分:3)

希望此代码段有用:

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