Last active
February 28, 2018 18:10
-
-
Save KazuyukiEguchi/ac521c75f0f5c125c562e921db795082 to your computer and use it in GitHub Desktop.
iOS11でCore NFCを使って、タグのIDを読み取る ref: http://qiita.com/KazuyukiEguchi/items/c3a92cf75689938d8e59
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2017-11-08 18:49:38.074386+0900 TestNFC[4847:694798] refreshPreferences: HangTracerEnabled: 0 | |
2017-11-08 18:49:38.074420+0900 TestNFC[4847:694798] refreshPreferences: HangTracerDuration: 500 | |
2017-11-08 18:49:38.074431+0900 TestNFC[4847:694798] refreshPreferences: ActivationLoggingEnabled: 0 ActivationLoggingTaskedOffByDA:0 | |
didDetectNDEFs | |
01:27:00:54:f2:e8:88:db | |
3 | |
goo.gl/OmhbM | |
didDetectNDEFs | |
04:6f:f3:c9:b7:02:80 | |
5 | |
didDetectNDEFs | |
04:e7:a4:b9:61:02:80 | |
5 | |
goo.gl/OmhbM | |
didInvalidateWithError: Session is invalidated due to maximum session timeout |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// TestNFC | |
// | |
// Created by Kazuyuki Eguchi on 2017/11/08. | |
// Copyright © 2017年 Kazuyuki Eguchi. All rights reserved. | |
// | |
import UIKit | |
import CoreNFC | |
class ViewController: UIViewController , NFCNDEFReaderSessionDelegate { | |
var session: NFCNDEFReaderSession? | |
func readerSession(_ session: NFCNDEFReaderSession, didInvalidateWithError error: Error) { | |
print("didInvalidateWithError: " + error.localizedDescription) | |
} | |
func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) { | |
print("didDetectNDEFs") | |
// UIDを取得する部分 | |
if(session.value(forKey: "_foundTags") != nil) | |
{ | |
let foundTags : NSArray = session.value(forKey: "_foundTags") as! NSArray | |
if foundTags.count > 0 { | |
let tag : NSObject = foundTags[0] as! NSObject | |
let uid : Data = tag.value(forKey: "_tagID") as! Data | |
let type : Int = tag.value(forKey: "_type") as! Int | |
var uid_s : String = "" | |
for (index,value) in uid.enumerated() { | |
var tmp = String(value, radix: 16); | |
if tmp.count == 1 { | |
tmp = "0" + tmp; | |
} | |
// Felica Liteの場合 | |
if type == 3 { | |
if index > 0 { | |
uid_s = uid_s + ":" | |
} | |
uid_s = uid_s + tmp | |
} | |
// type-Aの場合 | |
if type == 5 { | |
if index < 7 { | |
if index > 0 { | |
uid_s = uid_s + ":" | |
} | |
uid_s = uid_s + tmp | |
} | |
} | |
} | |
print(uid_s) | |
print(type) | |
} | |
} | |
// DEFの内容を取得する処理 | |
for message in messages { | |
for record in message.records { | |
if let mes = String.init(data: record.payload, encoding: .utf8) { | |
print(mes) | |
} | |
} | |
} | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
session = NFCNDEFReaderSession(delegate: self, queue: nil, invalidateAfterFirstRead: false) | |
session?.begin() | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment