NSURL 中的 AVMetadata 返回 NULL URL

AVMetadata in NSURL Returning NULL URL

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

URL =(空)

如果我将 detectionString 取出并用 "STORE100" 替换它,它工作正常并给我 URL 并连接到我的服务器。如果我按原样打印出 detectionString returns "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;
                    }

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

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