Skip to content

Commit fd9be81

Browse files
committed
ios: address more review comments, add more detailed error message
Signed-off-by: Shengqi Chen <[email protected]>
1 parent 3e5d62c commit fd9be81

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

.github/workflows/example-app.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
channel: 'stable'
4545
cache: true
4646

47-
- name: pre-build-script script for ${{ matrix.target }}
47+
- name: pre-build-script for ${{ matrix.target }}
4848
run: ${{ matrix.pre-build-script }}
4949

5050
- run: flutter pub get

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,5 +200,6 @@
200200
* ensure NFC Handler is always alive (#219 by @Akshya107)
201201
* prevent an NPE due to wrong API typing (#220)
202202
* add comment on `poll` related to Samsung API bug (#190, #200)
203+
* Add more detailed error message in iOS APIs
203204
* Bump tools to Gradle 9.2.1, AGP 8.13.0, Kotlin 2.2.21
204205
* Now `minSdkVersion` is lowered to 24 (#212)

ios/flutter_nfc_kit/Sources/flutter_nfc_kit/FlutterNfcKitPlugin.swift

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,12 @@ public class FlutterNfcKitPlugin: NSObject, FlutterPlugin, NFCTagReaderSessionDe
108108
case let .iso7816(tag):
109109
let apdu: NFCISO7816APDU? = NFCISO7816APDU(data: data)
110110
if apdu == nil {
111-
result(FlutterError(code: "400", message: "Command format error", details: nil))
111+
result(FlutterError(code: "400", message: "APDU format error", details: nil))
112112
return
113113
}
114114
tag.sendCommand(apdu: apdu!) { (response: Data, sw1: UInt8, sw2: UInt8, error: Error?) in
115115
if let error = error {
116-
result(FlutterError(code: "500", message: "Communication error", details: error.localizedDescription))
116+
result(FlutterError(code: "500", message: "Communication error with iso7816 tag", details: error.localizedDescription))
117117
} else {
118118
var response = response
119119
response.append(contentsOf: [sw1, sw2])
@@ -133,7 +133,7 @@ public class FlutterNfcKitPlugin: NSObject, FlutterPlugin, NFCTagReaderSessionDe
133133
// the first byte in data is length, and iOS will add it for us, so skip it
134134
tag.sendFeliCaCommand(commandPacket: data.advanced(by: 1)) { (response: Data, error: Error?) in
135135
if let error = error {
136-
result(FlutterError(code: "500", message: "Communication error", details: error.localizedDescription))
136+
result(FlutterError(code: "500", message: "Communication error with felica tag", details: error.localizedDescription))
137137
} else {
138138
if req is String {
139139
result(response.hexEncodedString())
@@ -145,7 +145,7 @@ public class FlutterNfcKitPlugin: NSObject, FlutterPlugin, NFCTagReaderSessionDe
145145
case let .miFare(tag):
146146
tag.sendMiFareCommand(commandPacket: data) { (response: Data, error: Error?) in
147147
if let error = error {
148-
result(FlutterError(code: "500", message: "Communication error", details: error.localizedDescription))
148+
result(FlutterError(code: "500", message: "Communication error with mifare tag", details: error.localizedDescription))
149149
} else {
150150
if req is String {
151151
result(response.hexEncodedString())
@@ -198,7 +198,7 @@ public class FlutterNfcKitPlugin: NSObject, FlutterPlugin, NFCTagReaderSessionDe
198198
let extendedMode = (arguments["iso15693ExtendedMode"] as? Bool) ?? false
199199
let handler = { (dataBlock: Data, error: Error?) in
200200
if let error = error {
201-
result(FlutterError(code: "500", message: "Communication error", details: error.localizedDescription))
201+
result(FlutterError(code: "500", message: "Cannot read iso15693 tag", details: error.localizedDescription))
202202
} else {
203203
result(dataBlock)
204204
}
@@ -213,10 +213,10 @@ public class FlutterNfcKitPlugin: NSObject, FlutterPlugin, NFCTagReaderSessionDe
213213
}
214214
else if case let .miFare(tag) = tag {
215215
let blockNumber = arguments["index"] as! UInt8
216-
let commandPacket = Data([0x30, blockNumber]) //0x30 is the MIFARE Classic Read Command.
216+
let commandPacket = Data([0x30, blockNumber]) // MiFARE Classic / Ultralight READ command
217217
tag.sendMiFareCommand(commandPacket: commandPacket) { (data, error) in
218218
if let error = error {
219-
result(FlutterError(code: "405", message: "Something is wrong", details: nil))
219+
result(FlutterError(code: "500", message: "Cannot read mifare tag", details: error.localizedDescription))
220220
} else {
221221
result(data)
222222
}
@@ -233,7 +233,7 @@ public class FlutterNfcKitPlugin: NSObject, FlutterPlugin, NFCTagReaderSessionDe
233233
let extendedMode = (arguments["iso15693ExtendedMode"] as? Bool) ?? false
234234
let handler = { (error: Error?) in
235235
if let error = error {
236-
result(FlutterError(code: "500", message: "Communication error", details: error.localizedDescription))
236+
result(FlutterError(code: "500", message: "Cannot write iso15693 tag", details: error.localizedDescription))
237237
} else {
238238
result(nil)
239239
}
@@ -247,19 +247,24 @@ public class FlutterNfcKitPlugin: NSObject, FlutterPlugin, NFCTagReaderSessionDe
247247
}
248248
}
249249
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: error.localizedDescription))
255-
}
256-
else {
257-
result(nil)
258-
}
259-
}
250+
let blockNumber = arguments["index"] as! UInt8
251+
let command = switch tag.mifareFamily {
252+
case .ultralight:
253+
0xA2 // MiFARE Ultralight WRITE command
254+
default:
255+
0xA0 // MiFARE Classic WRITE command
256+
} as UInt8
257+
let writeCommand = Data([command, blockNumber]) + data
258+
tag.sendMiFareCommand(commandPacket: writeCommand) { (response, error) in
259+
if let error = error {
260+
result(FlutterError(code: "500", message: "Cannot write mifare tag", details: error.localizedDescription))
261+
} else {
262+
result(nil)
263+
}
264+
}
260265
}
261266
else {
262-
result(FlutterError(code: "405", message: "writeBlock not supported on this type of card", details: nil))
267+
result(FlutterError(code: "405", message: "writeBlock not supported on this type of card", details: nil))
263268
}
264269
} else if call.method == "readNDEF" {
265270
if tag != nil {

0 commit comments

Comments
 (0)