diff --git a/README.md b/README.md index 4df50fb7..0f76f5c6 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/ios/NfcPlugin.m b/src/ios/NfcPlugin.m index 9b40f251..a530cd99 100644 --- a/src/ios/NfcPlugin.m +++ b/src/ios/NfcPlugin.m @@ -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 @@ -204,7 +204,7 @@ - (void)enabled:(CDVInvokedUrlCommand *)command { - (void) readerSession:(NFCNDEFReaderSession *)session didDetectNDEFs:(NSArray *)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]; } @@ -214,7 +214,7 @@ - (void) readerSession:(NFCNDEFReaderSession *)session didDetectNDEFs:(NSArray> *)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]; @@ -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; } @@ -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]; @@ -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; } @@ -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"); @@ -341,7 +341,7 @@ - (void)processNDEFTag: (NFCReaderSession *)session tag:(__kindof id [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; } @@ -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]; } @@ -395,19 +395,19 @@ - (void)readNDEFTag:(NFCReaderSession * _Nonnull)session status:(NFCNDEFStatus)s - (void)writeNDEFTag:(NFCReaderSession * _Nonnull)session status:(NFCNDEFStatus)status tag:(id)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]; @@ -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."]]; } } @@ -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