Search code examples
iosobjective-cqr-codezbar-sdkzbar

iOS ZBarSDK how to detect when user dismiss QR Scanner (ViewController)


In my code, I initialize ZBarReaderViewController for qr scanning.

I needed to detect when user quit the QR Scanning process without scanning any qr code. Is there any ways that I can know when user quit that process (by clicking on cancel button in the view) and execute something right after ZBarReaderViewController is dismissed?

Thank you in advance. Any info is much appreciated!

Below is my code to trigger qr scanning process by a click of a button.

-(IBAction)nextBtn:(id)sender{

     NSLog(@"trigger QR CODE");

     if(!reader)
     {
         reader = [[ZBarReaderViewController alloc]init];
         reader.readerDelegate = self;
         reader.supportedOrientationsMask = ZBarOrientationMaskAll;

         ZBarImageScanner *scanner = reader.scanner;
            // TODO: (optional) additional reader configuration here

            // EXAMPLE: disable rarely used I2/5 to improve performance
         [scanner setSymbology: ZBAR_I25
                     config: ZBAR_CFG_ENABLE
                             to: 0];

         UIView * infoButton = [[[[[reader.view.subviews objectAtIndex:2] subviews] objectAtIndex:0] subviews] objectAtIndex:3];

         [infoButton setHidden:YES];
     }

     // present and release the controller
     [self.view addSubview:spinner];
     [self.view setUserInteractionEnabled:NO];
     [spinner startAnimating];

     [self presentViewController:reader animated:YES completion:NULL];

}

This is how I capture the qr code when user scans one.

- (void) imagePickerController: (UIImagePickerController*) imageReader
  didFinishPickingMediaWithInfo: (NSDictionary*) info
 {
     // ADD: get the decode results
     id<NSFastEnumeration> results =
     [info objectForKey: ZBarReaderControllerResults];
     ZBarSymbol *symbol = nil;
     for(symbol in results)
         // EXAMPLE: just grab the first barcode
         break;


     NSString *receiptData = [NSString stringWithFormat:@"%@@-!-@%@", amountLabel.text, receiptLabel.text];

     isClosed = YES;
     NSLog(@"Completion");
     [reader dismissViewControllerAnimated:YES completion:^{
         [self BPUpdateTokenCollection:receiptData qrData:symbol.data];
         [reader.readerView stop];
          for(UIView *subViews in reader.view.subviews)
             [subViews removeFromSuperview];
         [reader.view removeFromSuperview];
         reader.view = nil;
         reader = nil;

      }];
  }

Solution

  • try this

    @protocol ZBarReaderDelegate <UIImagePickerControllerDelegate>
    
    
    
    ZBarReaderDelegate is nothing but UIImagePickerControllerDelegate
    
    - (void) imagePickerControllerDidCancel: (UIImagePickerController*) picker{
    
        //Cancelled 
    
        //Do your stuff here
    
    
    }