I would like put a @State var from my SwiftUIView in my UIViewController. My UIViewController is a scanner/reader qr code and when it detect QRCode i would like that my state var update automatically and assign my state var to the url found. I don't know where is the problem, here is the code :
ContentView.swift :
import SwiftUI
struct ContentView: View {
@State var url = "url"
var body: some View {
VStack {
SCRepresentable(test: $url)
Text(test)
.font(.title)
.foregroundColor(.white)
}.padding(.bottom, 2)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
ScannerViewController.swift (LOOK THE FUNCTION METADATOUTPUT where i try self.url = stringValue(the url found when qr code is detected)) :
import AVFoundation
import UIKit
import SwiftUI
class ScannerViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
var captureSession: AVCaptureSession!
var previewLayer: AVCaptureVideoPreviewLayer!
var url: String = "url"
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.black
captureSession = AVCaptureSession()
guard let videoCaptureDevice = AVCaptureDevice.default(for: .video) else { return }
let videoInput: AVCaptureDeviceInput
do {
videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
} catch {
return
}
if (captureSession.canAddInput(videoInput)) {
captureSession.addInput(videoInput)
} else {
failed()
return
}
let metadataOutput = AVCaptureMetadataOutput()
if (captureSession.canAddOutput(metadataOutput)) {
captureSession.addOutput(metadataOutput)
metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
metadataOutput.metadataObjectTypes = [.qr]
} else {
failed()
return
}
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.frame = view.layer.bounds
previewLayer.videoGravity = .resizeAspectFill
view.layer.addSublayer(previewLayer)
captureSession.startRunning()
}
func failed() {
let ac = UIAlertController(title: "Scanning not supported", message: "Your device does not support scanning a code from an item. Please use a device with a camera.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
captureSession = nil
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if (captureSession?.isRunning == false) {
captureSession.startRunning()
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if (captureSession?.isRunning == true) {
captureSession.stopRunning()
}
}
func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
captureSession.stopRunning()
if let metadataObject = metadataObjects.first {
guard let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject else { return }
guard let stringValue = readableObject.stringValue else { return }
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
self.url = stringValue
found(code: stringValue)
}
dismiss(animated: true)
}
func found(code: String) {
print(code)
print("url: ", self.url)
}
func rrr() -> String{
return self.url
}
override var prefersStatusBarHidden: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
}
SCRepresentable.swift :
import SwiftUI
struct SCRepresentable: UIViewControllerRepresentable {
@Binding var test: String
typealias UIViewControllerType = ScannerViewController
func makeUIViewController(context: UIViewControllerRepresentableContext<SCRepresentable>) -> ScannerViewController {
let scan = ScannerViewController()
test = scan.url
return scan
}
func updateUIViewController(_ uiViewController: ScannerViewController, context: UIViewControllerRepresentableContext<SCRepresentable>) {
//Add anything
}
}
Thanks for your help !
Hello there I see that you want to handle when the QR code detected you don't need an annotation @State, you need to bind the viewController with @Binding let me explain it more in code:
in ScannerViewController
we need to declare @Binding var urlDetected: String
that require to be initialized in init()
we call this func:
init(urlDetected: Binding<String>) {
_urlDetected = urlDetected
super.init(nibName: nil, bundle: nil)
}
that require to pass another binding variable in SCRepresentable
@Binding var urlDetected: String
func makeUIViewController(context: UIViewControllerRepresentableContext<SCRepresentable>) -> ScannerViewController {
let scan = ScannerViewController(isDetected: $urlDetected)
return scan
}
so now you have this var urlDetected
in SCRepresentable
when you declare it in the
struct ContentView: View {
@State var url = "url"
var body: some View {
VStack {
SCRepresentable(urlDetected: $url)
....<#Code#>
when the url inside the viewController changes it will notify and update the view
but don't forget to set a value inside the viewController like this self. urlDetected = .constant(stringValue