0

I created a simple barcode scan program that is launched from a website using a URL Scheme. It scans the barcode, then opens the browser back up with the data from the barcode. This works fine. However, I am trying to pass a parameter (Job Code) to the app so when the barcode is scanned, it opens the browser back open to the specific Job from the website link.

The Link on the website looks like this: MyNewApp://open-job?jobcode=1234

I am trying to get the jobcode.

Now, I can access it in the ContentView using the tutorial here: https://www.avanderlee.com/swiftui/deeplink-url-handling/

However, I don't need it in a View, I need it when a successful barcode is scanned in this function. So, How do I obtain the jobcode to be appended to the URL in this function?

Thank you so much for the help!

   func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {

        // Check if the metadataObjects array is not nil and it contains at least one object.
        if metadataObjects.count == 0 {
            scanResult = "No bar code detected"
            return
        }

        // Get the metadata object.
        let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject
        
        if metadataObj.type == AVMetadataObject.ObjectType.code39,
           
           let result = metadataObj.stringValue {
            scanResult = result
            print(scanResult)
            let theurl = String("https://backtomywebsite.com/jobdetails.php?source=app&vin=" + scanResult)
            if let link = URL(string: theurl) {
              UIApplication.shared.open(link)
            }
        }
    }

I have tried adding the jobcode to the View, then somehow launch the browser from there, but I received errors that I couldn't launch the browser from there. I used this function, but I haven't figured out how to get the function to read the incoming url scheme. Hopefully there is a simpler way to do this. I was thinking I could possibly edit the scheme and add Arguments Passed on Launch. I have seen answers that show how to do it with appDelegate, but I am using SwiftUI. Just a bit lost.

private func handleIncomingURL(_ url: URL) {
    guard url.scheme == "MyNewApp" else {
        return
    }
    guard let components = URLComponents(url: url, resolvingAgainstBaseURL: true) else {
        print("Invalid URL")
        return
    }

    guard let action = components.host, action == "open-job" else {
        print("Unknown URL, we can't handle this one!")
        return
    }

    guard let myjobcode = components.queryItems?.first(where: { $0.name == "jobcode" })?.value else {
        print("Job not found")
        return
    }

    openJobcode = myjobcode
}
  • 1
    You can use AppDelegate with SwiftUI. https://www.hackingwithswift.com/quick-start/swiftui/how-to-add-an-appdelegate-to-a-swiftui-app – jnpdx Jul 27 '23 at 17:02
  • I added the AppDelegate class to my file, Added a function in the AppDelegate to (hopefully) grab the url parameters. Added the Delegate Adapter @UIApplicationDelegateAdaptor(AppDelegate.self) var theappDelegate let myvar = theappDelegate.jobcode issue now...is that I cannot reference the variable jobcode. It is inside a function in the AppDelegate. How do I reference it in my struct? – Mike Crane Jul 28 '23 at 13:56
  • There really should be a much easier way to do this. – Mike Crane Jul 28 '23 at 14:02
  • Here’s one option: https://stackoverflow.com/questions/67439842/how-to-create-a-singleton-that-i-can-update-in-swiftui/67439890#67439890 – jnpdx Jul 28 '23 at 15:29

0 Answers0