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

Swift 5 support ? #160

Closed
ismailtosun opened this issue Jul 2, 2019 · 8 comments
Closed

Swift 5 support ? #160

ismailtosun opened this issue Jul 2, 2019 · 8 comments

Comments

@ismailtosun
Copy link

No description provided.

@zzarizz
Copy link

zzarizz commented Dec 20, 2019

or even SwiftUI, that would be awesome

@CLoutas
Copy link

CLoutas commented Jan 8, 2020

I am currently using BarcodeScanner in a SwiftUI project and haven't had any issues so far.

@ecamodeo
Copy link

@CLoutas I am trying to use BarcodeScanner in a SwiftUI project. I currently have it displaying using a wrapper but my delegates (code, error, dismiss) never run. Any advice you can give I would be greatful!

@CLoutas
Copy link

CLoutas commented Feb 13, 2020

@ecamodeo are you using a Coordinator object as your delegates? If not, have a look at this: https://www.hackingwithswift.com/books/ios-swiftui/using-coordinators-to-manage-swiftui-view-controllers

@ecamodeo
Copy link

ecamodeo commented Feb 13, 2020

@CLoutas I appreciate the time and apologize as I am kind of new to this whole thing but here is what I have for my Coordinator.
class Coordinator: NSObject, BarcodeScannerCodeDelegate, BarcodeScannerErrorDelegate, BarcodeScannerDismissalDelegate {`

func scanner(_ controller: BarcodeScannerViewController, didReceiveError error: Error) {
  print(error)
}

private func makeBarcodeScannerViewController() -> BarcodeScannerViewController {
  let viewController = BarcodeScannerViewController()
  viewController.codeDelegate = self
  viewController.errorDelegate = self
  viewController.dismissalDelegate = self
  return viewController
}

func scanner(_ controller: BarcodeScannerViewController, didCaptureCode code: String, type: String) {
  

    DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
        print("Barcode Data: \(code)")
        controller.dismiss(animated: true, completion: nil)
    }
}

func scannerDidDismiss(_ controller: BarcodeScannerViewController) {
  print("Dismissed!")
  controller.dismiss(animated: true, completion: nil)
}

}
`

@CLoutas
Copy link

CLoutas commented Feb 13, 2020

I'm having two objects, first the actual view (not full code, but should have everything you need):

struct ScannerView: UIViewControllerRepresentable {

func makeCoordinator() -> ScannerViewCoordinator {
    ScannerViewCoordinator(self)
}

func makeUIViewController(context: UIViewControllerRepresentableContext<ScannerView>) -> BarcodeScannerViewController {
    return createAndConfigureScanner(context: context)
}

func updateUIViewController(_ uiViewController: BarcodeScannerViewController, context: UIViewControllerRepresentableContext<ScannerView>) {
        uiViewController.reset(animated: false)
  }

 private func createAndConfigureScanner(context: UIViewControllerRepresentableContext<ScannerView>) -> BarcodeScannerViewController {
    let barcodeVC = BarcodeScannerViewController()
    barcodeVC.codeDelegate = context.coordinator
    barcodeVC.errorDelegate = context.coordinator
    
    return barcodeVC
}

And here is my coordinator (again, not full code):

class ScannerViewCoordinator: BarcodeScannerCodeDelegate, BarcodeScannerErrorDelegate {

private var scannerView: ScannerView

init(_ scannerView: ScannerView) {
    self.scannerView = scannerView
}

func scanner(_ controller: BarcodeScannerViewController, didCaptureCode code: String, type: String) {
    guard let scanItem = ScanParser().parse(content: code) else {
        controller.reset(animated: true)
        return
    }
    
    // Update the view with the scan result.
    self.scannerView.scanResult = scanItem
}

func scanner(_ controller: BarcodeScannerViewController, didReceiveError error: Error) {
    self.scannerView.scanFailed = true
}

}

Let me know if this helps!

@ecamodeo
Copy link

@CLoutas Thank you so much for your help and time! Works great.

@CLoutas
Copy link

CLoutas commented Feb 16, 2020

Perfect, happy to hear that helped! You’re welcome 🙂

@3lvis 3lvis closed this as completed Oct 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants