gpt4 book ai didi

ios - 从二维码读取VCFCard?

转载 作者:搜寻专家 更新时间:2023-11-01 07:16:30 24 4
gpt4 key购买 nike

我想问一下是否可以将VCF卡读取为CNContact数据,如givenName,familyName ...

这是我的QR Code Scanner输出函数

  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
}
}
}

打印输出

"
<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

"

最佳答案

我无法使用您的二维码进行测试,但这会有所帮助:

        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...
}

关于ios - 从二维码读取VCFCard?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41963677/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com