如何使用 swift 从 VCard 检索数据
How to retrieve data from VCard using swift
我正在研究二维码扫描。使用以下 swift 代码,QR 码中的数据将打印在控制台中。
import UIKit
import AVFoundation
class ViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate
{
let session = AVCaptureSession()
@IBOutlet weak var square: UIImageView!
var video = AVCaptureVideoPreviewLayer()
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
if metadataObjects != nil && metadataObjects.count != 0
{
if let object = metadataObjects[0] as? AVMetadataMachineReadableCodeObject
{
if object.type == AVMetadataObjectTypeQRCode
{
print(object.stringValue)
let alert = UIAlertController(title: "QR Code", message: object.stringValue, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Okey", style: .default, handler: nil))
/* alert.addAction(UIAlertAction(title: "Copy", style: .default, handler: { (nil) in
UIPasteboard.general.string = object.stringValue
}))
*/
present(alert, animated: true, completion: nil)
if metadataObjects.count > 0
{
print("-------------------------------------------------")
self.session.stopRunning()
}
}
}
}
}
@IBAction func startButton(_ sender: Any)
{
// Creating session
// let session = AVCaptureSession()
// Define capture device
let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
do {
let input = try AVCaptureDeviceInput(device: captureDevice)
session.addInput(input)
} catch {
print("Error")
}
let output = AVCaptureMetadataOutput()
session.addOutput(output)
output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
output.metadataObjectTypes = [AVMetadataObjectTypeQRCode]
video = AVCaptureVideoPreviewLayer(session: session)
video.frame = view.layer.bounds
view.layer.addSublayer(video)
self.view.bringSubview(toFront: square)
session.startRunning()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
输出:
BEGIN:VCARD
VERSION:2.1
FN:John Peter
N:Peter;John
TITLE:Admin
TEL;CELL:+91 431 524 2345
TEL;WORK;VOICE:+91 436 542 8374
EMAIL;WORK;INTERNET:John@ommail.in
URL:www.facebook.com
ADR;WORK:;;423 ofce sales Center;Newark;DE;3243;USA
ORG:xxx Private limited
END:VCARD
现在我要的是,具体如何获取VCard格式的数据,比如获取名字,姓氏,email id,手机号码等。之前也问过类似的问题,不过是在 objective-c。我不知道该怎么做 swift 3. 在此先感谢。
这是我使用的 Playground 中的示例代码。这不是最好的,但你会明白的:
import UIKit
import Contacts
var str = "BEGIN:VCARD \n" +
"VERSION:2.1 \n" +
"FN:John Peter \n" +
"N:Peter;John \n" +
"TITLE:Admin \n" +
"TEL;CELL:+91 431 524 2345 \n" +
"TEL;WORK;VOICE:+91 436 542 8374 \n" +
"EMAIL;WORK;INTERNET:John@ommail.in \n" +
"URL:www.facebook.com \n" +
"ADR;WORK:;;423 ofce sales Center;Newark;DE;3243;USA \n" +
"ORG:xxx Private limited \n" +
"END:VCARD"
if let data = str.data(using: .utf8) {
let contacts = try CNContactVCardSerialization.contacts(with: data)
let contact = contacts.first
print("\(String(describing: contact?.familyName))")
}
CNContactVCardSerialization.contacts(with: data)
的输出是 CNContact
(reference) 的数组。
在这里您可以查看 swift 4 of Mateusz Szlosek
的更新答案
var str = "BEGIN:VCARD \n" +
"VERSION:2.1 \n" +
"FN:John Peter \n" +
"N:Peter;John \n" +
"TITLE:Admin \n" +
"TEL;CELL:+91 431 524 2345 \n" +
"TEL;WORK;VOICE:+91 436 542 8374 \n" +
"EMAIL;WORK;INTERNET:John@ommail.in \n" +
"URL:www.facebook.com \n" +
"ADR;WORK:;;423 ofce sales Center;Newark;DE;3243;USA \n" +
"ORG:xxx Private limited \n" +
"END:VCARD"
func fectchContact(){
if let data = str.data(using: .utf8) {
do{
let contacts = try CNContactVCardSerialization.contacts(with: data)
let contact = contacts.first
print("\(String(describing: contact?.familyName))")
}
catch{
// Error Handling
print(error.localizedDescription)
}
}
}
我正在研究二维码扫描。使用以下 swift 代码,QR 码中的数据将打印在控制台中。
import UIKit
import AVFoundation
class ViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate
{
let session = AVCaptureSession()
@IBOutlet weak var square: UIImageView!
var video = AVCaptureVideoPreviewLayer()
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
if metadataObjects != nil && metadataObjects.count != 0
{
if let object = metadataObjects[0] as? AVMetadataMachineReadableCodeObject
{
if object.type == AVMetadataObjectTypeQRCode
{
print(object.stringValue)
let alert = UIAlertController(title: "QR Code", message: object.stringValue, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Okey", style: .default, handler: nil))
/* alert.addAction(UIAlertAction(title: "Copy", style: .default, handler: { (nil) in
UIPasteboard.general.string = object.stringValue
}))
*/
present(alert, animated: true, completion: nil)
if metadataObjects.count > 0
{
print("-------------------------------------------------")
self.session.stopRunning()
}
}
}
}
}
@IBAction func startButton(_ sender: Any)
{
// Creating session
// let session = AVCaptureSession()
// Define capture device
let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
do {
let input = try AVCaptureDeviceInput(device: captureDevice)
session.addInput(input)
} catch {
print("Error")
}
let output = AVCaptureMetadataOutput()
session.addOutput(output)
output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
output.metadataObjectTypes = [AVMetadataObjectTypeQRCode]
video = AVCaptureVideoPreviewLayer(session: session)
video.frame = view.layer.bounds
view.layer.addSublayer(video)
self.view.bringSubview(toFront: square)
session.startRunning()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
输出:
BEGIN:VCARD
VERSION:2.1
FN:John Peter
N:Peter;John
TITLE:Admin
TEL;CELL:+91 431 524 2345
TEL;WORK;VOICE:+91 436 542 8374
EMAIL;WORK;INTERNET:John@ommail.in
URL:www.facebook.com
ADR;WORK:;;423 ofce sales Center;Newark;DE;3243;USA
ORG:xxx Private limited
END:VCARD
现在我要的是,具体如何获取VCard格式的数据,比如获取名字,姓氏,email id,手机号码等。之前也问过类似的问题,不过是在 objective-c。我不知道该怎么做 swift 3. 在此先感谢。
这是我使用的 Playground 中的示例代码。这不是最好的,但你会明白的:
import UIKit
import Contacts
var str = "BEGIN:VCARD \n" +
"VERSION:2.1 \n" +
"FN:John Peter \n" +
"N:Peter;John \n" +
"TITLE:Admin \n" +
"TEL;CELL:+91 431 524 2345 \n" +
"TEL;WORK;VOICE:+91 436 542 8374 \n" +
"EMAIL;WORK;INTERNET:John@ommail.in \n" +
"URL:www.facebook.com \n" +
"ADR;WORK:;;423 ofce sales Center;Newark;DE;3243;USA \n" +
"ORG:xxx Private limited \n" +
"END:VCARD"
if let data = str.data(using: .utf8) {
let contacts = try CNContactVCardSerialization.contacts(with: data)
let contact = contacts.first
print("\(String(describing: contact?.familyName))")
}
CNContactVCardSerialization.contacts(with: data)
的输出是 CNContact
(reference) 的数组。
在这里您可以查看 swift 4 of Mateusz Szlosek
的更新答案 var str = "BEGIN:VCARD \n" +
"VERSION:2.1 \n" +
"FN:John Peter \n" +
"N:Peter;John \n" +
"TITLE:Admin \n" +
"TEL;CELL:+91 431 524 2345 \n" +
"TEL;WORK;VOICE:+91 436 542 8374 \n" +
"EMAIL;WORK;INTERNET:John@ommail.in \n" +
"URL:www.facebook.com \n" +
"ADR;WORK:;;423 ofce sales Center;Newark;DE;3243;USA \n" +
"ORG:xxx Private limited \n" +
"END:VCARD"
func fectchContact(){
if let data = str.data(using: .utf8) {
do{
let contacts = try CNContactVCardSerialization.contacts(with: data)
let contact = contacts.first
print("\(String(describing: contact?.familyName))")
}
catch{
// Error Handling
print(error.localizedDescription)
}
}
}