Skip to content

Commit 62f668c

Browse files
authored
Merge pull request #205 from rushank-shah/develop
Enable read & write block for iOS MIFARE Classic, Updated readme
2 parents 5756652 + c313e54 commit 62f668c

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ This plugin's functionalities include:
1212
* ISO 18092 (NFC-F / FeliCa)
1313
* ISO 15963 (NFC-V)
1414
* R/W block / page / sector level data of tags complying with:
15-
* MIFARE Classic / Ultralight (Android only)
15+
* MIFARE Classic / Ultralight (Android only, MIFARE Classic Read & Write block for iOS)
1616
* ISO 15693 (iOS only)
1717
* transceive raw commands with tags / cards complying with:
1818
* ISO 7816 Smart Cards (layer 4, in APDUs)

ios/flutter_nfc_kit/Sources/flutter_nfc_kit/FlutterNfcKitPlugin.swift

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,19 @@ public class FlutterNfcKitPlugin: NSObject, FlutterPlugin, NFCTagReaderSessionDe
210210
let blockNumber = arguments["index"] as! Int
211211
tag.extendedReadSingleBlock(requestFlags: RequestFlag(rawValue: rawFlags), blockNumber: blockNumber, completionHandler: handler)
212212
}
213-
} else {
213+
}
214+
else if case let .miFare(tag) = tag {
215+
let blockNumber = arguments["index"] as! UInt8
216+
let commandPacket = Data([0x30, blockNumber]) //0x30 is the MIFARE Classic Read Command.
217+
tag.sendMiFareCommand(commandPacket: commandPacket) { (data, error) in
218+
if let error = error {
219+
result(FlutterError(code: "405", message: "Something is wrong", details: nil))
220+
} else {
221+
result(data)
222+
}
223+
}
224+
}
225+
else {
214226
result(FlutterError(code: "405", message: "readBlock not supported on this type of card", details: nil))
215227
}
216228
} else if call.method == "writeBlock" {
@@ -233,8 +245,22 @@ public class FlutterNfcKitPlugin: NSObject, FlutterPlugin, NFCTagReaderSessionDe
233245
let blockNumber = arguments["index"] as! Int
234246
tag.extendedWriteSingleBlock(requestFlags: RequestFlag(rawValue: rawFlags), blockNumber: blockNumber, dataBlock: data, completionHandler: handler)
235247
}
236-
} else {
237-
result(FlutterError(code: "405", message: "writeBlock not supported on this type of card", details: nil))
248+
}
249+
else if case let .miFare(tag) = tag {
250+
let blockNumber = arguments["index"] as! UInt8
251+
let writeCommand = Data([0xA2, blockNumber]) + data //0xA2 is the MIFARE Classic Write Command to write single block.
252+
tag.sendMiFareCommand(commandPacket: writeCommand) { (response, error) in
253+
if let error = error {
254+
result(FlutterError(code: "500", message: "Communication error", details: nil))
255+
}
256+
else
257+
{
258+
result(nil)
259+
}
260+
}
261+
}
262+
else {
263+
result(FlutterError(code: "405", message: "writeBlock not supported on this type of card", details: nil))
238264
}
239265
} else if call.method == "readNDEF" {
240266
if tag != nil {

0 commit comments

Comments
 (0)