- 在info.plist中,加入Privacy - NFC Scan Usage Description
- 在Capabilities 加入Near Field Communication Tag Reading
- 掃描
func startScan() {
let session = NFCNDEFReaderSession(delegate: self, queue: nil, invalidateAfterFirstRead: false)
session.alertMessage = "Hold your iPhone near an NFC transit card."
session.begin()
}
- 連線,更新payload
func readerSession(_ session: NFCNDEFReaderSession, didDetect tags: [NFCNDEFTag]) {
guard
let sms = "場所代碼:111111111111111 本次實聯簡訊限防疫目的使用。".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed),
let payload = NFCNDEFPayload.wellKnownTypeURIPayload(string: "sms:1922&body=\(sms)"),
let tag = tags.first
else {
session.invalidate(errorMessage: "Could not process tag.")
return
}
session.connect(to: tag) { error in
guard error == nil else {
session.invalidate(errorMessage: "Could not connect to tag.")
return
}
tag.queryNDEFStatus { status, capacity, error in
guard error == nil else {
session.invalidate(errorMessage: "Could not query status of tag.")
return
}
switch status {
case .notSupported:
session.invalidate(errorMessage: "Tag is not supported.")
case .readOnly:
session.invalidate(errorMessage: "Tag is only readable.")
case .readWrite:
let messge = NFCNDEFMessage.init(records: [payload])
tag.writeNDEF(messge) { error in
if error != nil {
session.invalidate(errorMessage: "Failed to write message.")
} else {
session.alertMessage = "Successfully configured tag."
session.invalidate()
}
}
@unknown default: session.invalidate(errorMessage: "Unknown status of tag.")
}
}
}
}
2022年3月17日 星期四
用swift讀寫NTag
2022年3月5日 星期六
ARKit(SpriteKit) 臉部角度追蹤
- 建立😘 。由於前鏡頭左右相反,所以X軸要反向。
let labelNode = SKLabelNode(text: "😘")
labelNode.horizontalAlignmentMode = .center
labelNode.verticalAlignmentMode = .center
labelNode.xScale = -1
- 須將😘放入SKTransformNode,做後續的3D轉換。
let face = SKTransformNode()
face.addChild(labelNode)
self.face = face
- 更新時,SKTransformNode再依據ARFaceAnchor旋轉
let rotate = faceAnchor.transform.rotate
face?.setRotationMatrix(rotate)
- 最後,記得將node轉回來,否則貼圖會是反的
訂閱:
文章 (Atom)