简体   繁体   中英

Read VCFCard from QR Code?

I want to ask if is it possible to read VCF Card as CNContact data like givenName, familyName...

This is my QR Code Scanner output function

  func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {

    if metadataObjects == nil || metadataObjects.count == 0 {
        qrCodeFrameView?.frame = CGRect.zero
        messageLabel.text = "No QR/barcode is detected"
        return
    }


    let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject

    if supportedCodeTypes.contains(metadataObj.type) {
        let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj)
        qrCodeFrameView?.frame = barCodeObject!.bounds

        if metadataObj.stringValue != nil {
            messageLabel.text = metadataObj.stringValue
        }
    }
}

printed output

"
<AVMetadataMachineReadableCodeObject: 0x17022d1e0, type="org.iso.QRCode", bounds={ 0.3,0.4 0.1x0.3 }>corners { 0.3,0.7 0.5,0.7 0.5,0.4 0.3,0.4 }, time 93849323998125, stringValue "BEGIN:VCARD

VERSION:3.0

PRODID:-//Apple Inc.//iPhone OS 10.2//EN

N:Hank;Patrick;;;

FN: Patrick  Hank 

EMAIL;type=INTERNET;type=HOME;type=pref:patrick.hank01@gmail.com

TEL;type=IPHONE;type=CELL;type=VOICE;type=pref:0911311911

END:VCARD

"

I cannot test with your QR Code, but this would be some help:

        if
            let metadataString = metadataObj.stringValue,
            let data = metadataString.data(using: .utf8)
        {
            do {
                let contacts = try CNContactVCardSerialization.contacts(with: data)
                print(contacts)
                //Use `contacts` as you like
                if let contact = contacts.first {
                    print(contact.givenName)
                    //...
                }
                //...
            } catch {
                //Unreadable as CNContact
                print(error)
                //...
            }
        } else {
            //May never happen...
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM