【发布时间】:2019-09-13 22:22:20
【问题描述】:
我的代码似乎有问题。我将相机用作条形码扫描仪并将此返回的条形码解析到我的服务器。如果我将 detectionString 放入 NSURL 中,控制台中的结果如下所示以及一条警告消息,说我没有互联网连接 -
URL = (null)
如果我取出 detectionString 并将其替换为“STORE100”,它可以正常工作并为我提供 URL 并连接到我的服务器。如果我打印出检测字符串,它会返回“STORE100”。
大家有什么建议吗?
AVMetadataMachineReadableCodeObject *barCodeObject;
NSString *detectionString = nil;
NSArray *barCodeTypes = @[AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code,
AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode];
for (AVMetadataObject *metadata in metadataObjects) {
for (NSString *type in barCodeTypes) {
if ([metadata.type isEqualToString:type])
{
barCodeObject = (AVMetadataMachineReadableCodeObject *)[_prevLayer transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject *)metadata];
highlightViewRect = barCodeObject.bounds;
detectionString = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
break;
}
}
if ((detectionString != nil) && (counter<1))
{
if ([detectionString containsString:@"STORE"])
{
NSLog(@"testing STORE!!!");
counter++;
NSLog(@"testing!!! %@", detectionString);
NSString *test = detectionString;
NSURL *connectURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://grabngo.curtisboylan.com/api/storelookup.php?StoreID=%@", test]];
NSLog(@"URL = %@", connectURL);
NSData *jsonData = [NSData dataWithContentsOfURL:connectURL];
if(jsonData == nil)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Error" message:@"Please check your internet connection and try again." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
return;
}
【问题讨论】:
-
你是说如果你把
NSString *test = detectionString;改成NSString *test = @"STORE100";那么connectURL就不是nil?在此之前设置一个断点并逐步执行...仔细检查NSString *test = detectionString;确实分配了一个有效的字符串。 -
@DonMag 是的,这就是我要说的。我可以输出测试,我得到“STORE100”,或者我可以输出 detectionString,我也得到了。由于某种原因,NSURL 不喜欢它。
-
更改为:
NSString *test = [NSString stringWithFormat:@"https://grabngo.curtisboylan.com/api/storelookup.php?StoreID=%@", detectionString];,后跟:NSURL *connectURL = [NSURL URLWithString:test];... 逐步调试并检查值(不要依赖 NSLogs)。
标签: objective-c iphone nsurl avcapturesession stringwithformat