Skip to content
This repository has been archived by the owner on Oct 10, 2020. It is now read-only.

iOS: pressing cancel doesn't stop scanning #60

Closed
jakolion opened this issue Nov 16, 2018 · 15 comments · Fixed by #134
Closed

iOS: pressing cancel doesn't stop scanning #60

jakolion opened this issue Nov 16, 2018 · 15 comments · Fixed by #134

Comments

@jakolion
Copy link

This is my code:

String data = "";
try {
    data = await BarcodeScanner.scan();
} on PlatformException catch ( e ) {
    if ( e.code == BarcodeScanner.CameraAccessDenied ) {
        _log.warning( "camera permission not granted" );
        return;
    }
    else {
        _log.warning( "$e" );
        return;
    }
} on FormatException {
    // scan was canceled by user
    _log.info( "scan canceled by user" );
    return;
}
catch ( e ) {
    _log.warning( "$e" );
    return;
}

Pressing cancel when the camera is turned on, I expect the "FormatException". On Android this is working fine. On iOS, the flash is turned off, the camera screen is popped and the view is going back the the previous page as expected. But in the background the scanning is still active and the code above is still in line 3. When holding the phone over a QR code it is scanned successfully and the code above is continuing.

Tested with iOS 11.4 (15F79) on iPhone 5s and with iOS 12.1 (16B92) on iPhone 8.

@jakolion
Copy link
Author

jakolion commented Nov 21, 2018

Fixed with the mentioned merge request #55 and release 0.0.8. Can be closed.

@lauraseidler
Copy link

@jakolion do you get the FormatException on iOS? For me the scanner now stops and disappears, but I get neither an exception (not even in the last general catch block), nor a result.

@jakolion
Copy link
Author

@jakolion do you get the FormatException on iOS? For me the scanner now stops and disappears, but I get neither an exception (not even in the last general catch block), nor a result.

You are describing the behavior I had before version 0.0.8. The scanner disappeared and the previous page was shown but it was still running in the background.
With release 0.0.8 it worked the same way as before and throws the FormatException when canceling.
https://pub.dartlang.org/packages/barcode_scan/versions/0.0.8

@lauraseidler
Copy link

lauraseidler commented Feb 13, 2019

You are describing the behavior I had before version 0.0.8. The scanner disappeared and the previous page was shown but it was still running in the background.

Not quite - the scanner is off, as far as I can see, but I'm still not getting any Exception (or empty result). It feels like the Future is somehow not resolving at all (and I have 0.0.8).

@jakolion
Copy link
Author

Not quite - the scanner is off, as far as I can see, but I'm still not getting any Exception (or empty result). It feels like the Future is somehow not resolving at all (and I have 0.0.8).

After cancelling the scanner was still active in line 3 of my code above. There wasn't any result or exception as well. If you are really using version 0.0.8 then I don't have any idea. Currently it's working fine for me.

@theneedyguy
Copy link

I have the same issue. The app on iOS does not throw the format exception when I press cancel. Using 0.0.8 too.

@mdudek
Copy link

mdudek commented Mar 11, 2019

I'm facing same issue as @theneedyguy with version 0.0.8.

@ricpar11
Copy link

still having this error in version 1.0.0, any fix?

@JohanSnygg
Copy link

I have no way of testing this, but I may have a fix if someone could test it out and then commit it to the branch.

in the file BarcodeScannerViewController.m change the method "cancel" to:

- (void)cancel {
[self.delegate barcodeScannerViewController:self didFailWithErrorCode:@"USER_CANCELED"];
[self dismissViewControllerAnimated:true completion:nil];
}

and in barcode_scan.dart add:

static const UserCancelled = 'USER_CANCELED';

for use in exception handling around BarcodeScanner.scan();

Hope this helps

@duskmont
Copy link

duskmont commented Jun 9, 2019

I have the same issue when using 1.0.0

@ashkryab
Copy link

I have the same issue 1.0.0

@lookfirst
Copy link
Contributor

Ok, I figured it out, something just started working after resetting my codebase:

Per instructions from @JohanSnygg above. This seems pretty obvious now... cancel is called, but nothing is returned, so the Future in Dart never completes...

- (void)cancel {
    [self.delegate barcodeScannerViewController:self didFailWithErrorCode:@"USER_CANCELED"];
    [self dismissViewControllerAnimated:true completion:nil];
}

Now we can check for the PlatformException:

class BarcodeScanner {
  static const CameraAccessDenied = 'PERMISSION_NOT_GRANTED';
  static const UserCanceled = 'USER_CANCELED';
  static const MethodChannel _channel =
      const MethodChannel('com.apptreesoftware.barcode_scan');
  static Future<String> scan() async => await _channel.invokeMethod('scan');
}

I think this was the final goodness, change to copy (Also fixes warning in xcode)...

@property(nonatomic, copy) FlutterResult result;

@victor5171
Copy link

Hello! Do you have any ideas when this fixed version will be published at Dart's Repository?

@lookfirst
Copy link
Contributor

@victor5171

  barcode_scan:
    git: https://github.com/apptreesoftware/flutter_barcode_reader
    ref: 39ecc57bc0eef7b0465c891d900d2fb1f34a2390

@devtronic
Copy link
Member

@victor5171 A new version is available

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.