2

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

但是,对于我找到的教程,都只是显示带有警报的结果。

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

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

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

4

1 回答 1

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 
}
于 2013-09-06T10:16:50.650 回答