Skip to content

Commit c4c13be

Browse files
committed
Validation logic: Added IASKValidationResultOkWithReplacement
indicating that the validation is OK with the replacement performed; this is useful to auto-fix wrong content (the sample app adds an @ as the account name prefix);
1 parent cb414c7 commit c4c13be

File tree

5 files changed

+40
-1
lines changed

5 files changed

+40
-1
lines changed

InAppSettingsKit.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'InAppSettingsKit'
3-
s.version = '3.8.2'
3+
s.version = '3.8.3'
44
s.summary = 'This iPhone framework allows settings to be in-app in addition to being in the Settings app.'
55

66
s.description = <<-DESC

InAppSettingsKitSampleApp/Classes/MainViewController.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,22 @@ extension MainViewController: IASKSettingsDelegate {
9191
}
9292
textField.textColor = .red
9393
return .failed
94+
} else if key == "account_name", let value = textField.text {
95+
let regex = "^@?[\\w](?!.*?\\.{2})[\\w.]{1,28}[\\w]$"
96+
if value.isEmpty {
97+
return .ok
98+
} else if value == "@" {
99+
replacement?.pointee = "" as NSString
100+
return .failed
101+
} else if value.range(of: regex, options: .regularExpression) == nil {
102+
if let previousValue {
103+
replacement?.pointee = previousValue as NSString
104+
return .failedWithShake
105+
}
106+
} else if !value.hasPrefix("@") {
107+
replacement?.pointee = "@\(value)" as NSString
108+
return .okWithReplacement
109+
}
94110
}
95111
return .ok
96112
}

InAppSettingsKitSampleApp/Settings.bundle/Complete.plist

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,18 @@
304304
<key>IASKCellImage</key>
305305
<string>mail</string>
306306
</dict>
307+
<dict>
308+
<key>Type</key>
309+
<string>PSTextFieldSpecifier</string>
310+
<key>Title</key>
311+
<string>Account</string>
312+
<key>Key</key>
313+
<string>account_name</string>
314+
<key>AutocorrectionType</key>
315+
<string>No</string>
316+
<key>IASKCellImage</key>
317+
<string>person.crop.circle</string>
318+
</dict>
307319
<dict>
308320
<key>Type</key>
309321
<string>PSGroupSpecifier</string>

Sources/InAppSettingsKit/Controllers/IASKAppSettingsViewController.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,9 @@ - (void)textFieldDidEndEditing:(IASKTextField *)textField {
12041204
};
12051205

12061206
switch (result) {
1207+
case IASKValidationResultOkWithReplacement:
1208+
restoreText();
1209+
// fallthrough
12071210
case IASKValidationResultOk: {
12081211
if (![self.settingsStore objectForSpecifier:specifier] && textField.text.length == 0) {
12091212
return;

Sources/InAppSettingsKit/include/IASKAppSettingsViewController.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,16 @@ shouldPresentMailComposeViewController:(MFMailComposeViewController*)mailCompose
176176

177177
#pragma mark - Validation
178178
typedef NS_ENUM(NSUInteger, IASKValidationResult) {
179+
/** validation is OK, no replacement is performed. */
179180
IASKValidationResultOk,
181+
182+
/** validation is OK with the replacement performed. */
183+
IASKValidationResultOkWithReplacement,
184+
185+
/** validation has failed, the replacement is performed. */
180186
IASKValidationResultFailed,
187+
188+
/** validation has failed, the replacement is performed, and the field shakes to indicate the error. */
181189
IASKValidationResultFailedWithShake,
182190
};
183191
/** validate user input in text fields

0 commit comments

Comments
 (0)