Skip to content

Commit

Permalink
localization-update - Resolve NSLocalizedString macro collision.
Browse files Browse the repository at this point in the history
- Remove `NSLocalizedString` macro, as it was causing issues when leveraged from Swift code.
- Refactor calls to above macro to instead use `NSLocalizedStringFromTableInBundle` directly.
- Add `NSFaceIDUsageDescription` to example app `Info.plist`.
- Closes #39
- Closes #38
  • Loading branch information
Mike Stanziano committed Aug 26, 2019
1 parent 25a67f7 commit f7fcd67
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
2 changes: 2 additions & 0 deletions Example/DMPasscode/DMPasscode-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,7 @@
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>NSFaceIDUsageDescription</key>
<string>Authenticate to access locked feature</string>
</dict>
</plist>
2 changes: 1 addition & 1 deletion Pod/Assets/en.lproj/DMPasscodeLocalisation.strings
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
"dmpasscode_okay" = "Okay";
"dmpasscode_n_left" = "%i attempts left";
"dmpasscode_1_left" = "1 attempt left";
"dmpasscode_touchid_reason" = "Authenticate to access locked feature.";
"dmpasscode_touchid_reason" = "Authenticate to access locked feature.";
23 changes: 11 additions & 12 deletions Pod/Classes/DMPasscode.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
#import <LocalAuthentication/LocalAuthentication.h>
#endif

#undef NSLocalizedString
#define NSLocalizedString(key, comment) \
[bundle localizedStringForKey:(key) value:@"" table:@"DMPasscodeLocalisation"]

static DMPasscode* instance;
static const NSString* KEYCHAIN_NAME = @"passcode";
static NSBundle* bundle;
Expand Down Expand Up @@ -90,7 +86,8 @@ - (void)showPasscodeInViewController:(UIViewController *)viewController completi
_completion = completion;
LAContext* context = [[LAContext alloc] init];
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]) {
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:NSLocalizedString(@"dmpasscode_touchid_reason", nil) reply:^(BOOL success, NSError* error) {
NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass: [DMPasscode class]] pathForResource:@"DMPasscode" ofType:@"bundle"]];
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:NSLocalizedStringFromTableInBundle(@"dmpasscode_touchid_reason", @"DMPasscodeLocalisation", bundle, nil) reply:^(BOOL success, NSError* error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
switch (error.code) {
Expand Down Expand Up @@ -142,10 +139,11 @@ - (void)openPasscodeWithMode:(int)mode viewController:(UIViewController *)viewCo
DMPasscodeInternalNavigationController* nc = [[DMPasscodeInternalNavigationController alloc] initWithRootViewController:_passcodeViewController];
[nc setModalPresentationStyle:UIModalPresentationFormSheet];
[viewController presentViewController:nc animated:YES completion:nil];
NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass: [DMPasscode class]] pathForResource:@"DMPasscode" ofType:@"bundle"]];
if (_mode == 0) {
[_passcodeViewController setInstructions:NSLocalizedString(@"dmpasscode_enter_new_code", nil)];
[_passcodeViewController setInstructions:NSLocalizedStringFromTableInBundle(@"dmpasscode_enter_new_code", @"DMPasscodeLocalisation", bundle, nil)];
} else if (_mode == 1) {
[_passcodeViewController setInstructions:NSLocalizedString(@"dmpasscode_enter_to_unlock", nil)];
[_passcodeViewController setInstructions:NSLocalizedStringFromTableInBundle(@"dmpasscode_enter_to_unlock", @"DMPasscodeLocalisation", bundle, nil)];
}
}

Expand All @@ -157,19 +155,20 @@ - (void)closeAndNotify:(BOOL)success withError:(NSError *)error {

#pragma mark - DMPasscodeInternalViewControllerDelegate
- (void)enteredCode:(NSString *)code {
NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass: [DMPasscode class]] pathForResource:@"DMPasscode" ofType:@"bundle"]];
if (_mode == 0) {
if (_count == 0) {
_prevCode = code;
[_passcodeViewController setInstructions:NSLocalizedString(@"dmpasscode_repeat", nil)];
[_passcodeViewController setInstructions:NSLocalizedStringFromTableInBundle(@"dmpasscode_repeat", @"DMPasscodeLocalisation", bundle, nil)];
[_passcodeViewController setErrorMessage:@""];
[_passcodeViewController reset];
} else if (_count == 1) {
if ([code isEqualToString:_prevCode]) {
[[DMKeychain defaultKeychain] setObject:code forKey:KEYCHAIN_NAME];
[self closeAndNotify:YES withError:nil];
} else {
[_passcodeViewController setInstructions:NSLocalizedString(@"dmpasscode_enter_new_code", nil)];
[_passcodeViewController setErrorMessage:NSLocalizedString(@"dmpasscode_not_match", nil)];
[_passcodeViewController setInstructions:NSLocalizedStringFromTableInBundle(@"dmpasscode_enter_new_code", @"DMPasscodeLocalisation", bundle, nil)];
[_passcodeViewController setErrorMessage:NSLocalizedStringFromTableInBundle(@"dmpasscode_not_match", @"DMPasscodeLocalisation", bundle, nil)];
[_passcodeViewController reset];
_count = 0;
return;
Expand All @@ -180,9 +179,9 @@ - (void)enteredCode:(NSString *)code {
[self closeAndNotify:YES withError:nil];
} else {
if (_count == 1) {
[_passcodeViewController setErrorMessage:NSLocalizedString(@"dmpasscode_1_left", nil)];
[_passcodeViewController setErrorMessage:NSLocalizedStringFromTableInBundle(@"dmpasscode_1_left", @"DMPasscodeLocalisation", bundle, nil)];
} else {
[_passcodeViewController setErrorMessage:[NSString stringWithFormat:NSLocalizedString(@"dmpasscode_n_left", nil), 2 - _count]];
[_passcodeViewController setErrorMessage:[NSString stringWithFormat:NSLocalizedStringFromTableInBundle(@"dmpasscode_n_left", @"DMPasscodeLocalisation", bundle, nil), 2 - _count]];
}
[_passcodeViewController reset];
if (_count >= 2) { // max 3 attempts
Expand Down
7 changes: 7 additions & 0 deletions en.lproj/DMPasscodeLocalisation.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
DMPasscodeLocalisation.strings
Pods

Created by Mike Stanziano on 8/26/19.

*/

0 comments on commit f7fcd67

Please sign in to comment.