我想问一下是否可以将VCF卡作为CNContact数据读取,例如givenName,familyName ......
这是我的QR码扫描仪输出功能
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
"
答案 0 :(得分:1)
我无法使用您的QR码进行测试,但这会有所帮助:
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...
}