Uploaded image for project: 'Titanium SDK/CLI'
  1. Titanium SDK/CLI
  2. TIMOB-28267

Ti.Barcode event handlers not working properly on iOS

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Critical
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: Release 9.3.1
    • Component/s: iOS
    • Labels:
    • Environment:
    • Story Points:
      3
    • Sprint:
      2020 Sprint 25

      Description

      When using ti.barcode module with SDK 9.0.3.GA and 9.2.2.GA on iOS 13 it sometimes happens that the registered event handlers stop working. This often happens if you remove a event listener that does not exist and try adding it again.

      Because we want to ensure we don't register a event listener twice we often call `Barcode.removeEventListener(...);` before adding any listeners. This used to work on older iOS/titanium/ti.barcode versions.

      We've tested with ti.barcode version 2.0.4 and 6.0.0

      With the following example app the issue can be reproduced by tapping the buttons in the following order;

      > Add Event Listeners
      > resetCapture
      > remove event listeners
      > resetCapture

      The barcodeSuccess callback is not called anymore although that is expected.
      If we try to tap `resetCapture` multiple times in a clean run the event handers will always be properly removed, added and called.

      index.js

      const Barcode = require('ti.barcode');
      const overlay = Ti.UI.createView({
      	backgroundColor: 'transparent',
      	top: 0,
      	right: 0,
      	bottom: 0,
      	left: 0
      });
       
      var switchButton = Ti.UI.createButton({
      	title: Barcode.useFrontCamera ? 'Back Camera' : 'Front Camera',
      	textAlign: 'center',
      	color: '#000',
      	backgroundColor: '#fff',
      	style: 0,
      	font: {
      		fontWeight: 'bold',
      		fontSize: 16
      	},
      	borderColor: '#000',
      	borderRadius: 10,
      	borderWidth: 1,
      	opacity: 0.5,
      	width: 220,
      	height: 30,
      	bottom: 10
      });
       
      switchButton.addEventListener('click', function () {
      	Barcode.useFrontCamera = !Barcode.useFrontCamera;
      	switchButton.title = Barcode.useFrontCamera ? 'Back Camera' : 'Front Camera';
      });
       
      overlay.add(switchButton);
       
      var cancelButton = Ti.UI.createButton({
      	title: 'Cancel',
      	textAlign: 'center',
      	color: '#000',
      	backgroundColor: '#fff',
      	style: 0,
      	font: {
      		fontWeight: 'bold',
      		fontSize: 16
      	},
      	borderColor: '#000',
      	borderRadius: 10,
      	borderWidth: 1,
      	opacity: 0.5,
      	width: 220,
      	height: 30,
      	top: 20
      });
      cancelButton.addEventListener('click', function () {
      	Barcode.cancel();
      });
      overlay.add(cancelButton);
       
       
      function barcodeAddEventListeners() {
          console.log('barcodeAddEventListeners');
          Barcode.addEventListener('success', barcodeSuccess);
          Barcode.addEventListener('cancelled', barcodeCancelled);
          Barcode.addEventListener('error', barcodeError);
      }
       
      function barcodeRemoveEventListeners() {
          console.log('barcodeRemoveEventListeners');
          Barcode.removeEventListener('success', barcodeSuccess);
          Barcode.removeEventListener('cancelled', barcodeCancelled);
          Barcode.removeEventListener('error', barcodeError);
      }
       
      function resetCapture() {
          barcodeRemoveEventListeners();
          barcodeAddEventListeners();
          barcodeCapture();
      }
       
      function barcodeSuccess() {
          console.log('barcodeSuccess');
      }
       
      function barcodeCancelled() {
          console.log('barcodeCancelled');
      }
       
      function barcodeError() {
          console.log('barcodeError');
      }
       
       
      var cameraPermission = (callback) => {
      	if (OS_ANDROID) {
      		if (Ti.Media.hasCameraPermissions()) {
      			if (callback) {
      				callback(true);
      			}
      		} else {
      			Ti.Media.requestCameraPermissions(function (e) {
      				if (e.success) {
      					if (callback) {
      						callback(true);
      					}
      				} else {
      					if (callback) {
      						callback(false);
      					}
      					console.log('No camera permission'); // eslint-disable-line no-alert
      				}
      			});
      		}
      	}
       
      	if (OS_IOS) {
      		if (callback) {
      			callback(true);
      		}
      	}
      };
       
      function barcodeCapture() {
          cameraPermission(success => {
              console.log('gotPermission', {success: success});
              if(success) {
                  Barcode.capture({
                      animate: true,
                      showCancel: false,
                      showRectangle: false,
                      keepOpen: false,
                      allowInstructions: false,
                      allowMenu: true,
                      overlay: overlay,
                      acceptedFormats: [
                          Barcode.FORMAT_QR_CODE
                      ]
                  });
              }
          })
          
      }
       
       
      $.index.open();
      

      index.xml

      <Alloy>
      	<Window id="index" layout="vertical">		
      		<Button left="0" right="0" top="50" onClick="barcodeAddEventListeners">Add Event Listeners</Button>
      		<Button left="0" right="0" top="10" onClick="barcodeRemoveEventListeners">Remove Event Listeners</Button>
      		<Button left="0" right="0" top="10" onClick="barcodeCapture">barcodeCapture</Button>
      		<Button left="0" right="0" top="10" onClick="resetCapture">resetCapture</Button>
      	</Window>
      </Alloy>
      

        Attachments

          Activity

            People

            • Assignee:
              vijaysingh Vijay Singh
              Reporter:
              menzo VDLP
              Reviewer:
              Christopher Williams
              Tester:
              Satyam Sekhri
            • Watchers:
              8 Start watching this issue

              Dates

              • Created:
                Updated:

                Backbone Issue Sync

                • Backbone Issue Sync is enabled for your project, but we do not have any synchronization info for this issue.

                  Git Integration