从二维码读取VCFCard?
Read VCFCard from QR Code?
我想问一下是否可以将 VCF 卡读取为 CNContact 数据,如 givenName、familyName...
这是我的二维码扫描输出函数
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...
}
我想问一下是否可以将 VCF 卡读取为 CNContact 数据,如 givenName、familyName...
这是我的二维码扫描输出函数
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...
}