2017-01-31 125 views
1

我想問一下是不是可以讀取VCF卡像給定名稱,familyName CNContact數據...從QR碼中讀取VCFCard?

這是我的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:[email protected] 

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

END:VCARD 

" 
+0

你的代碼沒有'print',所以我不確定你的「打印輸出」是什麼。但是你可以嘗試'CNContactVCardSerialization'。 – OOPer

+0

我在MessageLabel.text下添加了...等。 ... print(metadataObj)..嘗試但不知道如何 – patrikbelis

回答

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

以及如何獲得例如givenName? – patrikbelis

+0

@patrikbelis,請查看[CNContact'的參考](https://developer.apple.com/reference/contacts/cncontact),'givenName'只是'CNContact'實例的一個屬性... – OOPer

+0

I意思是當我掃描vcfCard作爲QR碼...我怎樣才能得到givenName通過序列化? – patrikbelis