Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS alert message globalization #453

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ You must call [nfc.scanNdef](#nfcscanndef) and [nfc.scanTag](#nfcscantag) before

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.

To localize alert message strings use `Localizable.strings` files with these keys:
`"NFCHoldNearTag": "Hold near NFC tag to scan."`
`"NFCHoldNearWritableTag": "Hold near writable NFC tag to update."`
`"NFCMoreThanOneTag": "More than 1 tag detected. Please remove all tags and try again."`
`"NFCTagRead": "Tag successfully read."`
`"NFCDataWrote": "Wrote data to NFC tag."`
`"NFCDataWriteFailed": "Write failed."`
`"NFCDataReadFailed": "Read Failed."`
`"NFCReadOnlyTag": "Tag is read only."`
`"NFCUnknownNdefTag": "Unknown NDEF tag status."`
`"NFCNotNdefCompliant": "Tag is not NDEF compliant."`
`"NFCErrorTagStatus": "Error getting tag status."`
`"NFCErrorTagConnection": "Error connecting to tag."`

# NFC

> The nfc object provides access to the device's NFC sensor.
Expand Down
46 changes: 25 additions & 21 deletions src/ios/NfcPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,17 @@ - (void)writeTag:(CDVInvokedUrlCommand*)command API_AVAILABLE(ios(13.0)){
if (self.shouldUseTagReaderSession) {
NSLog(@"Using NFCTagReaderSession");

self.nfcSession = [[NFCTagReaderSession new]
self.nfcSession = [[NFCTagReaderSession alloc]
initWithPollingOption:(NFCPollingISO14443 | NFCPollingISO15693)
delegate:self queue:dispatch_get_main_queue()];

} else {
NSLog(@"Using NFCTagReaderSession");
self.nfcSession = [[NFCNDEFReaderSession new]initWithDelegate:self queue:nil invalidateAfterFirstRead:FALSE];
self.nfcSession = [[NFCNDEFReaderSession alloc]initWithDelegate:self queue:nil invalidateAfterFirstRead:FALSE];
}
}

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

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

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

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

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

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

Expand Down Expand Up @@ -306,22 +306,22 @@ - (void)startScanSession:(CDVInvokedUrlCommand*)command {

if (self.shouldUseTagReaderSession) {
NSLog(@"Using NFCTagReaderSession");
self.nfcSession = [[NFCTagReaderSession new]
self.nfcSession = [[NFCTagReaderSession alloc]
initWithPollingOption:(NFCPollingISO14443 | NFCPollingISO15693)
delegate:self queue:dispatch_get_main_queue()];
} else {
NSLog(@"Using NFCNDEFReaderSession");
self.nfcSession = [[NFCNDEFReaderSession new]initWithDelegate:self queue:nil invalidateAfterFirstRead:TRUE];
self.nfcSession = [[NFCNDEFReaderSession alloc]initWithDelegate:self queue:nil invalidateAfterFirstRead:TRUE];
}
sessionCallbackId = [command.callbackId copy];
self.nfcSession.alertMessage = @"Hold near NFC tag to scan.";
self.nfcSession.alertMessage = [self localizeString:@"NFCHoldNearTag" defaultValue:@"Hold near NFC tag to scan."];
[self.nfcSession beginSession];

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

Expand Down Expand Up @@ -379,11 +379,11 @@ - (void)readNDEFTag:(NFCReaderSession * _Nonnull)session status:(NFCNDEFStatus)s
// Error Code=403 "NDEF tag does not contain any NDEF message" is not an error for this plugin
if (error && error.code != 403) {
NSLog(@"%@", error);
[self closeSession:session withError:@"Read Failed."];
[self closeSession:session withError:[self localizeString:@"NFCDataReadFailed" defaultValue:@"Read Failed."]];
return;
} else {
NSLog(@"%@", message);
session.alertMessage = @"Tag successfully read.";
session.alertMessage = [self localizeString:@"NFCTagRead" defaultValue:@"Tag successfully read."];
[self fireNdefEvent:message metaData:metaData];
[self closeSession:session];
}
Expand All @@ -395,19 +395,19 @@ - (void)readNDEFTag:(NFCReaderSession * _Nonnull)session status:(NFCNDEFStatus)s
- (void)writeNDEFTag:(NFCReaderSession * _Nonnull)session status:(NFCNDEFStatus)status tag:(id<NFCNDEFTag>)tag API_AVAILABLE(ios(13.0)){
switch (status) {
case NFCNDEFStatusNotSupported:
[self closeSession:session withError:@"Tag is not NDEF compliant."]; // alternate message "Tag does not support NDEF."
[self closeSession:session withError:[self localizeString:@"NFCNotNdefCompliant" defaultValue:@"Tag is not NDEF compliant."]]; // alternate message "Tag does not support NDEF."
break;
case NFCNDEFStatusReadOnly:
[self closeSession:session withError:@"Tag is read only."];
[self closeSession:session withError:[self localizeString:@"NFCReadOnlyTag" defaultValue:@"Tag is read only."]];
break;
case NFCNDEFStatusReadWrite: {

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

}
default:
[self closeSession:session withError:@"Unknown NDEF tag status."];
[self closeSession:session withError:[self localizeString:@"NFCUnknownNdefTag" defaultValue:@"Unknown NDEF tag status."]];
}
}

Expand Down Expand Up @@ -619,4 +619,8 @@ - (NSString*) dictionaryAsJSONString:(NSDictionary *)dict {
return jsonString;
}

- (NSString*) localizeString:(NSString *)key defaultValue:(NSString*) defaultValue {
return NSLocalizedString(key, comment: "") != key ? NSLocalizedString(key, comment: "") : defaultValue;
}

@end