Skip to content

Commit 8f4a22b

Browse files
author
Raphael Stärk
committed
chariotsolutions#453 iOS alert message globalization
chariotsolutions#474 Fix Android 12 and above app crashes
1 parent d325b86 commit 8f4a22b

File tree

3 files changed

+49
-26
lines changed

3 files changed

+49
-26
lines changed

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PhoneGap NFC Plugin
1+
PhoneGap NFC Plugin (With Fix for Android 12 and above)
22
==========================
33

44
The NFC plugin allows you to read and write NFC tags. You can also beam to, and receive from, other NFC enabled devices.
@@ -72,6 +72,20 @@ You must call [nfc.scanNdef](#nfcscanndef) and [nfc.scanTag](#nfcscantag) before
7272

7373
Writing NFC tags on iOS uses the same [nfc.write](#nfcwrite) function as other platforms. Although it's the same function, the behavior is different on iOS. Calling `nfc.write` on an iOS device will start a new scanning session and write data to the scanned tag.
7474

75+
To localize alert message strings use `Localizable.strings` files with these keys:
76+
`"NFCHoldNearTag": "Hold near NFC tag to scan."`
77+
`"NFCHoldNearWritableTag": "Hold near writable NFC tag to update."`
78+
`"NFCMoreThanOneTag": "More than 1 tag detected. Please remove all tags and try again."`
79+
`"NFCTagRead": "Tag successfully read."`
80+
`"NFCDataWrote": "Wrote data to NFC tag."`
81+
`"NFCDataWriteFailed": "Write failed."`
82+
`"NFCDataReadFailed": "Read Failed."`
83+
`"NFCReadOnlyTag": "Tag is read only."`
84+
`"NFCUnknownNdefTag": "Unknown NDEF tag status."`
85+
`"NFCNotNdefCompliant": "Tag is not NDEF compliant."`
86+
`"NFCErrorTagStatus": "Error getting tag status."`
87+
`"NFCErrorTagConnection": "Error connecting to tag."`
88+
7589
# NFC
7690

7791
> The nfc object provides access to the device's NFC sensor.
@@ -1349,4 +1363,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13491363
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
13501364
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
13511365
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1352-
THE SOFTWARE.
1366+
THE SOFTWARE.

src/android/src/com/chariotsolutions/nfc/plugin/NfcPlugin.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,13 @@ private void createPendingIntent() {
482482
if (pendingIntent == null) {
483483
Activity activity = getActivity();
484484
Intent intent = new Intent(activity, activity.getClass());
485-
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
486-
pendingIntent = PendingIntent.getActivity(activity, 0, intent, 0);
485+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
486+
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
487+
pendingIntent = PendingIntent.getActivity(activity, 0, intent, PendingIntent.FLAG_IMMUTABLE);
488+
} else {
489+
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
490+
pendingIntent = PendingIntent.getActivity(activity, 0, intent, 0);
491+
}
487492
}
488493
}
489494

src/ios/NfcPlugin.m

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,17 @@ - (void)writeTag:(CDVInvokedUrlCommand*)command API_AVAILABLE(ios(13.0)){
125125
if (self.shouldUseTagReaderSession) {
126126
NSLog(@"Using NFCTagReaderSession");
127127

128-
self.nfcSession = [[NFCTagReaderSession new]
128+
self.nfcSession = [[NFCTagReaderSession alloc]
129129
initWithPollingOption:(NFCPollingISO14443 | NFCPollingISO15693)
130130
delegate:self queue:dispatch_get_main_queue()];
131131

132132
} else {
133133
NSLog(@"Using NFCTagReaderSession");
134-
self.nfcSession = [[NFCNDEFReaderSession new]initWithDelegate:self queue:nil invalidateAfterFirstRead:FALSE];
134+
self.nfcSession = [[NFCNDEFReaderSession alloc]initWithDelegate:self queue:nil invalidateAfterFirstRead:FALSE];
135135
}
136136
}
137137

138-
self.nfcSession.alertMessage = @"Hold near writable NFC tag to update.";
138+
self.nfcSession.alertMessage = [self localizeString:@"NFCHoldNearWritableTag" defaultValue:@"Hold near writable NFC tag to update."];
139139
sessionCallbackId = [command.callbackId copy];
140140

141141
if (reusingSession) { // reusing a read session to write
@@ -204,7 +204,7 @@ - (void)enabled:(CDVInvokedUrlCommand *)command {
204204
- (void) readerSession:(NFCNDEFReaderSession *)session didDetectNDEFs:(NSArray<NFCNDEFMessage *> *)messages API_AVAILABLE(ios(11.0)) {
205205
NSLog(@"NFCNDEFReaderSession didDetectNDEFs");
206206

207-
session.alertMessage = @"Tag successfully read.";
207+
session.alertMessage = [self localizeString:@"NFCTagRead" defaultValue:@"Tag successfully read."];
208208
for (NFCNDEFMessage *message in messages) {
209209
[self fireNdefEvent: message];
210210
}
@@ -214,7 +214,7 @@ - (void) readerSession:(NFCNDEFReaderSession *)session didDetectNDEFs:(NSArray<N
214214
- (void) readerSession:(NFCNDEFReaderSession *)session didDetectTags:(NSArray<__kindof id<NFCNDEFTag>> *)tags API_AVAILABLE(ios(13.0)) {
215215

216216
if (tags.count > 1) {
217-
session.alertMessage = @"More than 1 tag detected. Please remove all tags and try again.";
217+
session.alertMessage = [self localizeString:@"NFCMoreThanOneTag" defaultValue:@"More than 1 tag detected. Please remove all tags and try again."];
218218
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 500 * NSEC_PER_MSEC), dispatch_get_main_queue(), ^{
219219
NSLog(@"restaring polling");
220220
[session restartPolling];
@@ -227,7 +227,7 @@ - (void) readerSession:(NFCNDEFReaderSession *)session didDetectTags:(NSArray<__
227227
[session connectToTag:tag completionHandler:^(NSError * _Nullable error) {
228228
if (error) {
229229
NSLog(@"%@", error);
230-
[self closeSession:session withError:@"Error connecting to tag."];
230+
[self closeSession:session withError:[self localizeString:@"NFCErrorTagConnection" defaultValue:@"Error connecting to tag."]];
231231
return;
232232
}
233233

@@ -262,7 +262,7 @@ - (void)tagReaderSession:(NFCTagReaderSession *)session didDetectTags:(NSArray<_
262262
NSLog(@"tagReaderSession didDetectTags");
263263

264264
if (tags.count > 1) {
265-
session.alertMessage = @"More than 1 tag detected. Please remove all tags and try again.";
265+
session.alertMessage = [self localizeString:@"NFCMoreThanOneTag" defaultValue:@"More than 1 tag detected. Please remove all tags and try again."];
266266
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 500 * NSEC_PER_MSEC), dispatch_get_main_queue(), ^{
267267
NSLog(@"restaring polling");
268268
[session restartPolling];
@@ -277,7 +277,7 @@ - (void)tagReaderSession:(NFCTagReaderSession *)session didDetectTags:(NSArray<_
277277
[session connectToTag:tag completionHandler:^(NSError * _Nullable error) {
278278
if (error) {
279279
NSLog(@"%@", error);
280-
[self closeSession:session withError:@"Error connecting to tag."];
280+
[self closeSession:session withError:[self localizeString:@"NFCErrorTagConnection" defaultValue:@"Error connecting to tag."]];
281281
return;
282282
}
283283

@@ -306,22 +306,22 @@ - (void)startScanSession:(CDVInvokedUrlCommand*)command {
306306

307307
if (self.shouldUseTagReaderSession) {
308308
NSLog(@"Using NFCTagReaderSession");
309-
self.nfcSession = [[NFCTagReaderSession new]
309+
self.nfcSession = [[NFCTagReaderSession alloc]
310310
initWithPollingOption:(NFCPollingISO14443 | NFCPollingISO15693)
311311
delegate:self queue:dispatch_get_main_queue()];
312312
} else {
313313
NSLog(@"Using NFCNDEFReaderSession");
314-
self.nfcSession = [[NFCNDEFReaderSession new]initWithDelegate:self queue:nil invalidateAfterFirstRead:TRUE];
314+
self.nfcSession = [[NFCNDEFReaderSession alloc]initWithDelegate:self queue:nil invalidateAfterFirstRead:TRUE];
315315
}
316316
sessionCallbackId = [command.callbackId copy];
317-
self.nfcSession.alertMessage = @"Hold near NFC tag to scan.";
317+
self.nfcSession.alertMessage = [self localizeString:@"NFCHoldNearTag" defaultValue:@"Hold near NFC tag to scan."];
318318
[self.nfcSession beginSession];
319319

320320
} else if (@available(iOS 11.0, *)) {
321321
NSLog(@"iOS < 13, using NFCNDEFReaderSession");
322-
self.nfcSession = [[NFCNDEFReaderSession new]initWithDelegate:self queue:nil invalidateAfterFirstRead:TRUE];
322+
self.nfcSession = [[NFCNDEFReaderSession alloc]initWithDelegate:self queue:nil invalidateAfterFirstRead:TRUE];
323323
sessionCallbackId = [command.callbackId copy];
324-
self.nfcSession.alertMessage = @"Hold near NFC tag to scan.";
324+
self.nfcSession.alertMessage = [self localizeString:@"NFCHoldNearTag" defaultValue:@"Hold near NFC tag to scan."];
325325
[self.nfcSession beginSession];
326326
} else {
327327
NSLog(@"iOS < 11, no NFC support");
@@ -341,7 +341,7 @@ - (void)processNDEFTag: (NFCReaderSession *)session tag:(__kindof id<NFCNDEFTag>
341341
[tag queryNDEFStatusWithCompletionHandler:^(NFCNDEFStatus status, NSUInteger capacity, NSError * _Nullable error) {
342342
if (error) {
343343
NSLog(@"%@", error);
344-
[self closeSession:session withError:@"Error getting tag status."];
344+
[self closeSession:session withError:[self localizeString:@"NFCErrorTagStatus" defaultValue:@"Error getting tag status."]];
345345
return;
346346
}
347347

@@ -379,11 +379,11 @@ - (void)readNDEFTag:(NFCReaderSession * _Nonnull)session status:(NFCNDEFStatus)s
379379
// Error Code=403 "NDEF tag does not contain any NDEF message" is not an error for this plugin
380380
if (error && error.code != 403) {
381381
NSLog(@"%@", error);
382-
[self closeSession:session withError:@"Read Failed."];
382+
[self closeSession:session withError:[self localizeString:@"NFCDataReadFailed" defaultValue:@"Read Failed."]];
383383
return;
384384
} else {
385385
NSLog(@"%@", message);
386-
session.alertMessage = @"Tag successfully read.";
386+
session.alertMessage = [self localizeString:@"NFCTagRead" defaultValue:@"Tag successfully read."];
387387
[self fireNdefEvent:message metaData:metaData];
388388
[self closeSession:session];
389389
}
@@ -395,19 +395,19 @@ - (void)readNDEFTag:(NFCReaderSession * _Nonnull)session status:(NFCNDEFStatus)s
395395
- (void)writeNDEFTag:(NFCReaderSession * _Nonnull)session status:(NFCNDEFStatus)status tag:(id<NFCNDEFTag>)tag API_AVAILABLE(ios(13.0)){
396396
switch (status) {
397397
case NFCNDEFStatusNotSupported:
398-
[self closeSession:session withError:@"Tag is not NDEF compliant."]; // alternate message "Tag does not support NDEF."
398+
[self closeSession:session withError:[self localizeString:@"NFCNotNdefCompliant" defaultValue:@"Tag is not NDEF compliant."]]; // alternate message "Tag does not support NDEF."
399399
break;
400400
case NFCNDEFStatusReadOnly:
401-
[self closeSession:session withError:@"Tag is read only."];
401+
[self closeSession:session withError:[self localizeString:@"NFCReadOnlyTag" defaultValue:@"Tag is read only."]];
402402
break;
403403
case NFCNDEFStatusReadWrite: {
404404

405405
[tag writeNDEF: self.messageToWrite completionHandler:^(NSError * _Nullable error) {
406406
if (error) {
407407
NSLog(@"%@", error);
408-
[self closeSession:session withError:@"Write failed."];
408+
[self closeSession:session withError:[self localizeString:@"NFCDataWriteFailed" defaultValue:@"Write failed."]];
409409
} else {
410-
session.alertMessage = @"Wrote data to NFC tag.";
410+
session.alertMessage = [self localizeString:@"NFCDataWrote" defaultValue:@"Wrote data to NFC tag."];
411411
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
412412
[self.commandDelegate sendPluginResult:pluginResult callbackId:self->sessionCallbackId];
413413
[self closeSession:session];
@@ -417,7 +417,7 @@ - (void)writeNDEFTag:(NFCReaderSession * _Nonnull)session status:(NFCNDEFStatus)
417417

418418
}
419419
default:
420-
[self closeSession:session withError:@"Unknown NDEF tag status."];
420+
[self closeSession:session withError:[self localizeString:@"NFCUnknownNdefTag" defaultValue:@"Unknown NDEF tag status."]];
421421
}
422422
}
423423

@@ -619,4 +619,8 @@ - (NSString*) dictionaryAsJSONString:(NSDictionary *)dict {
619619
return jsonString;
620620
}
621621

622-
@end
622+
- (NSString*) localizeString:(NSString *)key defaultValue:(NSString*) defaultValue {
623+
return NSLocalizedString(key, comment: "") != key ? NSLocalizedString(key, comment: "") : defaultValue;
624+
}
625+
626+
@end

0 commit comments

Comments
 (0)