クリップボードにコピーする
UIPasteboard.general.string = "コピーする文字列"
QRコードを生成する
func makeQRCode(text: String) -> UIImage? {
guard let data = text.data(using: .utf8) else { return nil }
let qr = CIFilter(name: "CIQRCodeGenerator", parameters: ["inputMessage": data])!
return UIImage(ciImage: qr.outputImage!)
}
完成したコード
DatabaseHelper.swift
func createRoom(userID:String){
db.collection("room").addDocument(data: ["user":[userID,uid]])
}
AddFriendView.swift
import Foundation
import UIKit
class AddFriendView:UIViewController {
@IBOutlet weak var idField: UITextField!
@IBOutlet weak var qrView: UIImageView!
@IBOutlet weak var idLabel: UILabel!
let uid = AuthHelper().uid()
let database = DatabaseHelper()
override func viewDidLoad() {
super.viewDidLoad()
idLabel.text = "MyID:\(uid)"
qrView.image = makeQRCode(text: uid)
}
@IBAction func onSearch(_ sender: Any) {
conform(id: idField.text!)
}
@IBAction func onCopy(_ sender: Any) {
UIPasteboard.general.string = uid
}
func conform(id:String){
database.getUserInfo(userID: id, result: {
result in
if result == "" {
self.showError(message: "存在しないidです。")
} else {
self.performSegue(withIdentifier: "conform", sender: UserData(id: id, name: result))
}
})
}
func showError(message:String){
let dialog = UIAlertController(title: "エラー", message: message, preferredStyle: .alert)
dialog.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(dialog, animated: true, completion: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "conform"{
let vc = segue.destination as! FriendConformView
let data = sender as! UserData
vc.userID = data.id
vc.name = data.name
}
}
func makeQRCode(text: String) -> UIImage? {
guard let data = text.data(using: .utf8) else { return nil }
let qr = CIFilter(name: "CIQRCodeGenerator", parameters: ["inputMessage": data, "inputCorrectionLevel": "H"])!
return UIImage(ciImage: qr.outputImage!)
}
}
class FriendConformView:UIViewController {
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var imageView: UIImageView!
var userID = ""
var name = ""
var database = DatabaseHelper()
override func viewDidLoad() {
super.viewDidLoad()
nameLabel.text = name
database.getImage(userID: userID, imageView: imageView)
imageView.layer.cornerRadius = imageView.frame.size.height * 0.5
imageView.clipsToBounds = true
}
@IBAction func onAdd(_ sender: Any) {
database.createRoom(userID: userID)
dismiss(animated: true, completion: nil)
}
@IBAction func onCancel(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
}
コメント