0

I'm using the camera view as a barcode scanner that scans a barcode and launches a segue to another viewcontroller with a webview. This is working fine, and I can navigate back to the scanner from the webview and scan another barcode without issue. However, if I navigate away from the view controller with the camera and return to it, the camera view loads but no longer detects barcodes.

@implementation ProductScanViewController

NSString *loadUrl;     
AVCaptureSession *_captureSession;
AVCaptureDevice *_videoDevice;
AVCaptureDeviceInput *_videoInput;
AVCaptureVideoPreviewLayer *_previewLayer;
BOOL _running;
AVCaptureMetadataOutput *_metadataOutput;
@synthesize mWebView;


- (void)viewDidLoad {
    [super viewDidLoad];
NSURL *url = [NSURL URLWithString:loadUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {if([data length] > 0 && error == nil)[mWebView loadRequest:request];         else if (error != nil) NSLog(@"Error: %", error);}
 ];

    [self setupCaptureSession];
}

- (void)setupCaptureSession {

    // 1
    if (_captureSession){        
        return;
    }

    // 2
    _videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if (!_videoDevice) {
        return;
    }

    // 3
    _captureSession = [[AVCaptureSession alloc] init];

    // 4
    _videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:_videoDevice error:nil];

    // 5
    if ([_captureSession canAddInput:_videoInput]) {
        [_captureSession addInput:_videoInput];
    }

    // 6
    _previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
    _previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

    // capture and process the metadata
    _metadataOutput = [[AVCaptureMetadataOutput alloc] init];
    dispatch_queue_t metadataQueue =
    dispatch_queue_create("com.1337labz.featurebuild.metadata", 0);
    [_metadataOutput setMetadataObjectsDelegate:self
                                      queue:metadataQueue];
    if ([_captureSession canAddOutput:_metadataOutput]) {
        [_captureSession addOutput:_metadataOutput];
    }
}

#pragma mark - Delegate functions

- (void)captureOutput:(AVCaptureOutput *)captureOutput
    didOutputMetadataObjects:(NSArray *)metadataObjects
       fromConnection:(AVCaptureConnection *)connection {

    [metadataObjects enumerateObjectsUsingBlock:^(AVMetadataObject *obj,
                               NSUInteger idx,
                              BOOL *stop) {
         if ([obj isKindOfClass: [AVMetadataMachineReadableCodeObject class]]) {
             //NSLog(@"Capture Output started");

            // 3
            AVMetadataMachineReadableCodeObject *code = (AVMetadataMachineReadableCodeObject*)
            [_previewLayer transformedMetadataObjectForMetadataObject:obj];

            // 4
            Barcode * barcode = [Barcode processMetadataObject:code];

            for (NSString * str in self.allowedBarcodeTypes) {
                 if([barcode.getBarcodeType isEqualToString:str]){
                     [self validBarcodeFound:(barcode)];
                     return;
                 }
             }
         }
    }];
}

- (void) validBarcodeFound:(Barcode *)barcode{
NSLog(@"Found Barcode");

[self stopRunning];
[self.foundBarcodes addObject:barcode];
//[self showBarcodeAlert:barcode];
NSString *alertMessage = @"";
alertMessage = [alertMessage stringByAppendingString:[barcode getBarcodeType]];
NSLog([barcode getBarcodeData]);
NSLog(alertMessage);

NSLog([NSString stringWithFormat:@"%@", barcode.getBarcodeData]);
if ([barcode.getBarcodeType isEqualToString:@"org.iso.QRCode"])
{
 if ([[NSString stringWithFormat:@"%lu",(unsigned long)[self.foundBarcodes count]-1] length] > 0){

    NSString *input = [barcode getBarcodeData];
    [NSString stringWithFormat:@"%lu",(unsigned long)[self.foundBarcodes count]-1];
    NSLog(input);


    if ([input length] >= 13)
    {
        input = [input substringToIndex:12];
    }
    loadUrl = [[@"http://www.mywebsite.co.uk/" stringByAppendingString:input] stringByAppendingString:@"?utm_source=iphone"];
    NSLog(loadUrl);        
    dispatch_sync(dispatch_get_main_queue(), ^{
        [self performSegueWithIdentifier:@"toWebView" sender:self];
    });
}


}
tonyony
  • 15
  • 5
  • When you found bar code? What do you do? – Rajesh Maurya Jun 17 '15 at 13:22
  • Are you navigating your view controller to another view controller? – Rajesh Maurya Jun 17 '15 at 13:29
  • Yes, it launches a segue to a webview embedded in another viewcontroller. – tonyony Jun 17 '15 at 13:39
  • When you come back it will nil your object, check all object. – Rajesh Maurya Jun 17 '15 at 14:04
  • I can go to and from the webview without it causing a problem. It's only when I navigate back through the stack from the barcode scanner that it stops working. – tonyony Jun 17 '15 at 14:12
  • The navigation is not clear. In your class you declare a WebView, but you say the WebView is in another controller, then you say you navigate back from the barcode scanner. If the scanner is removed, it can not work, please explain this more detailed. – Christian Jun 17 '15 at 14:26
  • Instead of `viewDidLoad` have you tried `viewWillAppear` as the place to put your `setupCaptureSession` call? – Russell Austin Jun 17 '15 at 14:27
  • @RussellAustin The camera doesn't load when it gets called in `viewWillAppear`. @Christian I've added some more code to clarify the navigation. – tonyony Jun 17 '15 at 14:57

1 Answers1

0

So I've managed to fix this but can't give any real understanding of why it works.

Instead of loading the URL asynchronously during viewDidLoad, I passed the URL with the segue and loaded it from within the ViewController containing the WebView.

Further to this, the variable declarations were encapsulated in curly brackets {} and the @synthesize mWebView was removed. I've no idea why that was causing issues so any possible explanation would be appreciated

tonyony
  • 15
  • 5