2017-10-30 01:05:11 +08:00
|
|
|
#import "BarcodeScanPlugin.h"
|
2017-11-08 10:27:25 +08:00
|
|
|
#import "BarcodeScannerViewController.h"
|
2017-10-30 01:05:11 +08:00
|
|
|
|
|
|
|
@implementation BarcodeScanPlugin
|
|
|
|
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
|
2017-11-08 10:27:25 +08:00
|
|
|
FlutterMethodChannel *channel = [FlutterMethodChannel methodChannelWithName:@"com.apptreesoftware.barcode_scan"
|
|
|
|
binaryMessenger:registrar.messenger];
|
|
|
|
BarcodeScanPlugin *instance = [BarcodeScanPlugin new];
|
|
|
|
instance.hostViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
|
|
|
|
[registrar addMethodCallDelegate:instance channel:channel];
|
2017-10-30 01:05:11 +08:00
|
|
|
}
|
2017-11-08 10:27:25 +08:00
|
|
|
|
|
|
|
- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
|
|
|
|
if ([@"scan" isEqualToString:call.method]) {
|
|
|
|
self.result = result;
|
|
|
|
[self showBarcodeView];
|
|
|
|
} else {
|
|
|
|
result(FlutterMethodNotImplemented);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)showBarcodeView {
|
|
|
|
BarcodeScannerViewController *scannerViewController = [[BarcodeScannerViewController alloc] init];
|
|
|
|
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:scannerViewController];
|
|
|
|
scannerViewController.delegate = self;
|
|
|
|
[self.hostViewController presentViewController:navigationController animated:NO completion:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)barcodeScannerViewController:(BarcodeScannerViewController *)controller didScanBarcodeWithResult:(NSString *)result {
|
2018-01-10 23:20:43 +08:00
|
|
|
if (self.result) {
|
2017-11-08 10:27:25 +08:00
|
|
|
self.result(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 23:25:40 +08:00
|
|
|
- (void)barcodeScannerViewController:(BarcodeScannerViewController *)controller didFailWithErrorCode:(NSString *)errorCode {
|
2018-01-10 23:20:43 +08:00
|
|
|
if (self.result){
|
|
|
|
self.result([FlutterError errorWithCode:errorCode
|
|
|
|
message:nil
|
|
|
|
details:nil]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-30 01:05:11 +08:00
|
|
|
@end
|