0

I am new to Xcode / Swift and have been following examples online to add a settings bundle to my app.

What I am struggling with is how to limit the number of characters a user can enter in a settings text field. The root.plist does not seem to have this as an option and this answer seems to be for a textfield within the app rather than one that is changed in settings.

Please can someone point me in the right direction.

Chris
  • 4,009
  • 3
  • 21
  • 52
  • Why are the `shouldChangeCharactersIn` answers being downvoted? – koen Dec 02 '19 at 14:42
  • @koen I think it must be because this question relates to the settings bundle, where you cannot set up delegates etc. I ran into this problem myself and was told it is impossible so have provided an answer like that. Do you know of a way to do it? – Chris Dec 02 '19 at 14:46
  • Ah, I see. Unfortunately I would not know how to do that. But what would be a scenario where the user is using a textField in the settings bundle to change `root.plist`? What does that look like in the app? – koen Dec 02 '19 at 15:44
  • 1
    @koen The developer can use the settings root.plist to specify the type of control in the iOS settings page for that app (text, switch for Bool etc). The values are then available to the app programmatically from User Defaults. You have no control over the UI in the iOS settings app other than the title for and type of controls used. – Chris Dec 03 '19 at 13:28
  • @Chris So for instance WiFi settings where you can type in your credentials? – koen Dec 03 '19 at 13:40
  • @koen It’s a bit like that. Here is an image from one of my apps showing a submenu of my app’s settings as they appear in the iOS settings app. The only bits I can control are the titles, available options, type of control and the name of the User Defaults property each one links to. https://www.dropbox.com/s/iwznh61gu2x4yts/IMG_8461.PNG?dl=1 – Chris Dec 03 '19 at 13:57

3 Answers3

0

Unfortunately this cannot be done for text entered in the settings bundle. You do not have control over this.

You could parse the result within your app when loaded from UserDefaults and make changes then if needed, possibly with an alert to the user.

The settings root.plist will let you add footer text below controls, so you could at least provide information for user letting them know the maximum length.

An alternative option would be to use a custom settings page within your app, with a UITextField. Then use the UITextFieldDelegate method shouldChangeCharactersIn to control text entry length.

Chris
  • 4,009
  • 3
  • 21
  • 52
  • 1
    Thanks - I'll try the custom settings page route. This has at least saved me hours of work trying to get something to work that wasn't even possible :-) – Nick Milner Dec 02 '19 at 14:38
  • @NickMilner Thanks. I had wondered about this myself once, and could never find a way. You have very little control over the settings bundle. I ended up changing the values in the app itself rather than making a custom page. If you’re using SwiftUI I think the `Form` struct makes settings pages and the like fairly straightforward. – Chris Dec 02 '19 at 14:45
  • That's the only valid answer, that should be accepted. – Frederik Winkelsdorf Feb 01 '20 at 15:06
  • @FrederikA.Winkelsdorf Thanks – Chris Feb 01 '20 at 15:52
  • 1
    @Chris You're welcome. New iOS developers likely don't understand that system provided elements like inside the settings are just not adjustable. It's not allowed, wanted or possible (beside Jailbreaks or sometimes private API). Nothing to discuss about. – Frederik Winkelsdorf Feb 01 '20 at 16:09
-1

set delegate of your TextField to self and conform your ViewController to UITextFieldDelegate

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        guard let textFieldText = textField.text,
            let rangeOfTextToReplace = Range(range, in: textFieldText) else {
                return false
        }
        let substringToReplace = textFieldText[rangeOfTextToReplace]
        let count = textFieldText.count - substringToReplace.count + string.count
        return count <= 10  //number of characters 
    }
M Ohamed Zead
  • 197
  • 10
  • 1
    what do you mean by changing it in settings @Nick Milner – M Ohamed Zead Dec 02 '19 at 14:25
  • You cannot use a delegate (or any custom code) in the settings bundle, so this would not be possible. This is fine if it is in the app, but there is limited control of the settings bundle and no custom code can be used. – Chris Dec 03 '19 at 17:54
-1

You need to set the delegate of textField and then you have to implement the method mentioned as below

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
   let text = (textField.text as NSString?)?.replacingCharacters(in: range, with: string) ?? ""
   return text.count <= 10 // your count 
}
koen
  • 5,383
  • 7
  • 50
  • 89