美文网首页
iOS11 NFC 读卡Demo

iOS11 NFC 读卡Demo

作者: xnxin | 来源:发表于2017-06-12 17:56 被阅读82次

iOS11 NFC 读卡Demo

注意,NFC开发现需要付费开发者才能使用


0x01 准备工作

  • 创建一个App ID,并勾选NFC Tag Reading

  • 创建应用,并使用刚才创建的App ID

  • plist文件中加入以下几行

<key>NFCReaderUsageDescription</key>
<string>NFC Tag!</string>
  • 生成CardCollections.entitlements
    Capabilities中打开Push Notifications然后关闭会生成一份工程名字.entitlements,内容填写如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>com.apple.developer.nfc.readersession.formats</key>
    <array>
        <string>NDEF</string>
    </array>
</dict>
</plist>

然后前往Build Settings中设置Code Signing Entitlements的路径,务必保证路径正确

0x02 编写代码

import UIKit
import CoreNFC


class ViewController: UIViewController {
    
    
    var session: NFCNDEFReaderSession!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        session = NFCNDEFReaderSession(delegate: self,
                                       queue: DispatchQueue(label: "queueName", attributes: .concurrent), invalidateAfterFirstRead: false)
        session.begin()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

extension ViewController: NFCNDEFReaderSessionDelegate {
    func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {
        for message in messages {
            for record in message.records {
                print(record.payload)
            }
        }
    }
    
    func readerSession(_ session: NFCNDEFReaderSession, didInvalidateWithError error: Error) {
        print("read error:\(error)")
    }
}

相关文章

  • iOS11 NFC 读卡Demo

    iOS11 NFC 读卡Demo 注意,NFC开发现需要付费开发者才能使用 0x01 准备工作 创建一个App I...

  • iOS11 Core NFC

    iOS11 Core NFC iPhone6开始支持NFC(Near Field Communication )...

  • iOS11新特性NFC检测

    Core NFC是在iOS11中引入,用于处理NFC阅读Tag。目前NFC只开启了读权限,据分析应该是为了Appl...

  • NFC读卡判断卡类型

    如题,项目中会用到NFC读取卡号,并且需要获取卡的类型。这时候我们可以在获取nfc读卡的onNewIntent方法...

  • NFC的使用

    一、NFC的使用范围 苹果在iOS11上推出了NFC的功能,开发者可以根据自身的需要使用这个功能进行开发。NFC有...

  • NFC的使用说明

    一、NFC的使用范围 苹果在iOS11上推出了NFC的功能,开发者可以根据自身的需要使用这个功能进行开发。NFC有...

  • 如何用NFC手机模拟门禁卡

    一、软硬件准备 NFC Tool 手机上的IC卡读写编辑软件,搭配蓝牙读卡器或者OTG读卡器,可实现在手机上破解、...

  • NFC手机模拟门禁卡

    一、软硬件准备 NFC Tool 手机上的IC卡读写编辑软件,搭配蓝牙读卡器或者OTG读卡器,可实现在手机上破解、...

  • android nfc demo

    我这边只有mifare卡,这个demo也是针对m1卡编写的 当android扫描到NFC标签时,会自动寻找最适合的...

  • iOS11的NFC干起正紧事来太疯狂

    最近,iOS11的NFC炒的火热,或许大家都等着iOS的NFC登场,然后,小伙伴们一起组队去刷iPhone公交卡了...

网友评论

      本文标题:iOS11 NFC 读卡Demo

      本文链接:https://www.haomeiwen.com/subject/bpzlqxtx.html