Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix document camera not available crash #69

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions ios/Classes/SwiftCunningDocumentScannerPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import VisionKit

@available(iOS 13.0, *)
public class SwiftCunningDocumentScannerPlugin: NSObject, FlutterPlugin, VNDocumentCameraViewControllerDelegate {
var resultChannel :FlutterResult?
var resultChannel: FlutterResult?
var presentingController: VNDocumentCameraViewController?

public static func register(with registrar: FlutterPluginRegistrar) {
Expand All @@ -18,9 +18,13 @@ public class SwiftCunningDocumentScannerPlugin: NSObject, FlutterPlugin, VNDocum
if call.method == "getPictures" {
let presentedVC: UIViewController? = UIApplication.shared.keyWindow?.rootViewController
self.resultChannel = result
self.presentingController = VNDocumentCameraViewController()
self.presentingController!.delegate = self
presentedVC?.present(self.presentingController!, animated: true)
if VNDocumentCameraViewController.isSupported {
self.presentingController = VNDocumentCameraViewController()
self.presentingController!.delegate = self
presentedVC?.present(self.presentingController!, animated: true)
} else {
result(FlutterError(code: "UNAVAILABLE", message: "Document camera is not available on this device", details: nil))
}
} else {
result(FlutterMethodNotImplemented)
return
Expand All @@ -41,7 +45,7 @@ public class SwiftCunningDocumentScannerPlugin: NSObject, FlutterPlugin, VNDocum
df.dateFormat = "yyyyMMdd-HHmmss"
let formattedDate = df.string(from: currentDateTime)
var filenames: [String] = []
for i in 0 ... scan.pageCount - 1 {
for i in 0 ..< scan.pageCount {
let page = scan.imageOfPage(at: i)
let url = tempDirPath.appendingPathComponent(formattedDate + "-\(i).png")
try? page.pngData()?.write(to: url)
Expand All @@ -57,7 +61,7 @@ public class SwiftCunningDocumentScannerPlugin: NSObject, FlutterPlugin, VNDocum
}

public func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFailWithError error: Error) {
resultChannel?(nil)
resultChannel?(FlutterError(code: "ERROR", message: error.localizedDescription, details: nil))
presentingController?.dismiss(animated: true)
}
}
Loading