NSURL中的AVMetadata返回NULL URL

时间:2019-04-24 15:00:27

标签: objective-c iphone nsurl avcapturesession stringwithformat

我的代码似乎有问题。我将相机用作条形码扫描仪,并将返回的条形码解析到我的服务器。如果我将detectionString放入NSURL,则控制台中的结果如下,并显示一条警告消息,说我没有Internet连接-

URL =(空)

如果我拿出detectionString并将其替换为“ STORE100”,它将正常工作并提供URL并与服务器连接。如果我打印出detectionString,它会返回“ 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;
                    }

1 个答案:

答案 0 :(得分:0)

在将扫描的条形码传递到NSURL之前,您应该尝试对其进行URL编码。可能无法在扫描的代码中创建一些无法打印的特殊字符,从而无法创建有效的NSURL:

NSURL *connectURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://grabngo.curtisboylan.com/api/storelookup.php?StoreID=%@",[test stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]]];