Ibrahim
Ibrahim

Reputation: 9

Opening an URL scanned from a QR code in SFSafariViewController

I'm trying to make my app open the scanned url from qr codes. I made a qr code scanner but it only copies the value of the scanned qr code to the pasteboard. I'm trying to make it open it in a SFSafariViewController. instead of "Copy" option, I want to make it "Open" and actually open the scanned url.

Here's my code

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
    if metadataObjects != nil && metadataObjects.count != 0
    {
        if let object = metadataObjects[0] as? AVMetadataMachineReadableCodeObject
        {
            if object.type == AVMetadataObjectTypeQRCode
            {
                let alert = UIAlertController(title: "QR Code", message: object.stringValue, preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "Retake", style: .default, handler: nil))
                alert.addAction(UIAlertAction(title: "Copy", style: .default, handler: { (nil) in
                    UIPasteboard.general.string = object.stringValue

                }))
                present(alert, animated: true, completion: nil)

            }
        }
    }

}

Upvotes: 1

Views: 1105

Answers (2)

Pm Abi
Pm Abi

Reputation: 245

Using safariviewController is pretty straightforward

First you have to import safariservices and then present the safaricontroller with a url

The basic codes to this are

import SafariServices

func loadSafari(url : String){
  guard let url = URL(string: url) else { return }

  let safariController = SFSafariViewController(url: url)
  present(safariController, animated: true, completion: nil)
}

place this code in your class and call the function inside your capture output

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
if metadataObjects != nil && metadataObjects.count != 0
{
    if let object = metadataObjects[0] as? AVMetadataMachineReadableCodeObject
    {
        if object.type == AVMetadataObjectTypeQRCode
        {

                UIPasteboard.general.string = object.stringValue
                loadSafari(url: object.stringValue)

            }))
            present(alert, animated: true, completion: nil)

        }
    }
}

}

clicking on the done will dismiss the safariController and have the user navigate back to the previous viewcontroller.

I hope this helps. Let me know how it goes.

Upvotes: 1

Hodson
Hodson

Reputation: 3556

Your code shows no sign of attempting to use the SFSafariViewController. Have you tried something like the below?

import SafariServices

if let url = URL(string: object.stringValue) {
    let browser = SFSafariViewController(url: url)
}

This won't make it magically appear, there will still be some work you need to do yourself to present it to the user such as:

presentViewController(browser, animated: true, completion: nil)

Upvotes: 0

Related Questions