NSURL中的AVMetadata返回NULL URL

问题描述 投票:0回答:1

我的代码似乎有问题。我正在使用相机作为条形码扫描仪并将此返回的条形码解析到我的服务器。如果我将detectString放入NSURL,控制台中的结果如下以及一条警告消息说我没有互联网连接 -

URL =(null)

如果我取出detectString并将其替换为“STORE100”,它工作正常并给我URL并连接我的服务器。如果我打印出detectString,它会返回“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;
                    }
objective-c iphone nsurl avcapturesession stringwithformat
1个回答
0
投票

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

NSURL *connectURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://grabngo.curtisboylan.com/api/storelookup.php?StoreID=%@",[test stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]]];
© www.soinside.com 2019 - 2024. All rights reserved.