objective-ciphonensurlavcapturesessionstringwithformat

AVMetadata in NSURL Returning NULL URL


I seem to have an issue with my code. I am using the camera as a barcode scanner and parsing this returned barcode to my server. If I place detectionString into the NSURL the result in the console is as follows along with an alert message saying I have no internet connection -

URL = (null)

If I take detectionString out and replace it with "STORE100" it works fine and gives me the URL and connects with my server. If I print the detectionString out it returns "STORE100" as it should.

Anyone any suggestions?

 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;
                    }

Solution

  • You should try url encoding the scanned barcode before passing it to NSURL. Possibly there are some non printable special characters in the scanned code failing creation of a valid NSURL:

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