我需要在我的应用程序中嵌入QR码阅读器。到目前为止,ZBarSDK似乎是ios的很好解决方案。我已经掌握了一些示例和教程,并将功能成功嵌入到我的应用程序中。
但是,对于我发现的所有教程,都只显示警告结果。
就我而言,我将要使用的QR码将包含一个网址,我需要的是在扫描后直接显示网页。需要在包含导航栏的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];
但我不知道什么是实现此功能的正确代码。
最佳答案
希望此代码段有助于:
- (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
}
关于iphone - 如何通过ZBarSDK从QR码获取URL并将其直接呈现在UIWebView中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18655130/