About Me

My photo
Drum & Bass Producer, Software Developer, Love my Cats

iOS 10 Swift 3 How to limit input in a UITextField to Digits only (or any other set of characters) - Swift 3 Digits-only text field

If you want to create a UITextField that allows only inputting specific characters, simply set your viewcontroller as the text field's delegate and implement this delegate method:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if string.characters.count > 0, !"1234567890".contains(string) {
            return false
// the rest of your logic goes here...


notice that here I'm allowing only any of these chars: "1234567890"
but your can change the string to any character set you want, for example "abcdefgh"(...) etc

notice the negative logic here: if this string is NOT contained in the character set you provided - then return false; else - do the rest of your logic..

[edit:] also, notice the first if:  string.characters.count > 0 which does not prevent deleting characters from the textfield (without this if-condition, you'll prevent your user from backspacing)

Also, it is best practice, of course, to choose the appropriate keyboard type in Interface Builder - based on what you want your user to input:
help your user

How to fix this message in iOS 10 development: [App] if we're in the real pre-commit handler we can't actually add any new fences due to CA restriction

This message started popping up in my console a lot, here's how to fix it:

1. click the run-scheme you're debugging
2. click "Edit Scheme..."
3. in the "Run (debug)" tab - pick "Arguments"
4. click + in the "Environment Variables" to add an argument
5. enter "OS_ACTIVITY_MODE" as the name
6. enter "disabled" as the value
7. click close, clean project and run again!

this is how to solve this message on xcode in ios 10

[App] if we're in the real pre-commit handler we can't actually add any new fences due to CA restriction

let me know if it was helpful in the buttons below!

simple elegant way to get user agent in iOS swift 3 using a webview and a tiny bit of javascript

Here's a simple and elegant way to add a user-agent header to your headers dictionary in whatever networking framework you're using, if it doesn't provide the user-agent header out-of-the-box:

"User-Agent" : UIWebView().stringByEvaluatingJavaScript(from: "navigator.userAgent")!

we just init a webview, run a javascript that returns the user-agent from within the webview as a string, and use the value wherever you want (in the headers, eg)

have fun getting device user agent in swift!

how to use custom or external fonts in swift ios app

if you want to use external or custom fonts in a swift ios app, just:
1. copy the font files into the project
2. in your info.plist file, add an entry called "Fonts provided by application" (xcode will autocomplete you)
3. now you have an array, in which you add the names of the fonts - each in its own string entry

and that's how you add custom fonts to your ios app - good luck!

how to limit UITextField number of characters

When you wan t to limit the number of characters that a user can input to a uitextfield, you can use this uitextfielddelegate method:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate { // make sure you inherit uitextfielddelegate

    @IBOutlet weak var textField: UITextField! // your textfield
    let limit = 10

    override func viewDidLoad() {
        textField.delegate = self // set myself as the delegate

// this method "listens" to changes in the textfield, and you can return true or false as appropriate
    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        guard let text = textField.text else { return true }
        let newLength = text.characters.count + string.characters.count - range.length
        return newLength <= limit