Skip to content

Commit

Permalink
chariotsolutions#453 iOS alert message globalization
Browse files Browse the repository at this point in the history
chariotsolutions#474 Fix Android 12 and above app crashes
  • Loading branch information
Raphael Stärk committed Aug 16, 2022
1 parent d325b86 commit 8f4a22b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 26 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PhoneGap NFC Plugin
PhoneGap NFC Plugin (With Fix for Android 12 and above)
==========================

The NFC plugin allows you to read and write NFC tags. You can also beam to, and receive from, other NFC enabled devices.
Expand Down 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 Expand Up @@ -1349,4 +1363,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE.
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,13 @@ private void createPendingIntent() {
if (pendingIntent == null) {
Activity activity = getActivity();
Intent intent = new Intent(activity, activity.getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(activity, 0, intent, 0);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(activity, 0, intent, PendingIntent.FLAG_IMMUTABLE);
} else {
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(activity, 0, intent, 0);
}
}
}

Expand Down
48 changes: 26 additions & 22 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;
}

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

@end

0 comments on commit 8f4a22b

Please sign in to comment.