1

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;

      }];
  }
Soumya Ranjan
  • 4,817
  • 2
  • 26
  • 51
IssacZH.
  • 1,457
  • 3
  • 24
  • 54
  • 1
    if you can deploy only on iOS7 is worth to mention that the last version of iOS includes barcode/qrcode detection API, easy to integrate and pretty reliable. If you can't there is also another library that is a user friendly wrapper around zxing https://github.com/TheLevelUp/ZXingObjC – Andrea May 20 '14 at 09:17
  • 1
    Currently I am targeting iOS 7 and above. So basically this would be a great alternative way to implement QR scanner. Thanks for the info – IssacZH. May 20 '14 at 09:25
  • 1
    A little tutorial to get how it can be implemented http://www.shinobicontrols.com/blog/posts/2013/10/11/ios7-day-by-day-day-16-decoding-qr-codes-with-avfoundation – Andrea May 20 '14 at 09:26

1 Answers1

1

try this

@protocol ZBarReaderDelegate <UIImagePickerControllerDelegate>



ZBarReaderDelegate is nothing but UIImagePickerControllerDelegate

- (void) imagePickerControllerDidCancel: (UIImagePickerController*) picker{

    //Cancelled 

    //Do your stuff here


}