How to detect the country code and change flag in FPNTextField

Viewed 1752

I am using FlagPhoneno Pod to search or select country codes with flags, by using below codes. But when comes to edit part I get full phone no to the FPNtextFeild ex- +971568573570 and by default i set country code as .AE

I want to detect and automatically change the country flag when any other country phone no fetch from json let recivedPhoneNo = self.json["phone_number"].string ?? ""

For the moment the flag is changing when select only. please show me an example method with code from same pod "FlagPhoneno" module.

I am using following code.

    func setupPhoneTF(){
        self.phoneTFView.layer.borderColor = UIColor.appColor.customGray.cgColor
        self.phoneTFView.layer.cornerRadius = 5
        self.phoneTFView.layer.borderWidth = 1
        
                phoneTF.displayMode = .list // .picker by default
                listController.setup(repository: phoneTF.countryRepository)

                listController.didSelect = { [weak self] country in
                    self?.phoneTF.setFlag(countryCode: country.code)
                }

                phoneTF.delegate = self
                phoneTF.font = UIFont.systemFont(ofSize: 14)

                // Custom the size/edgeInsets of the flag button
                phoneTF.flagButtonSize = CGSize(width: 35, height: 35)
                phoneTF.flagButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
                 let items = [
                UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.save, target: self, action: nil),
                UIBarButtonItem(title: "Item 1", style: .plain, target: self, action: nil),
                UIBarButtonItem(title: "Item 2", style: .plain, target: self, action: nil)
            ]
                phoneTF.textFieldInputAccessoryView = getCustomTextFieldInputAccessoryView(with: items)
                phoneTF.hasPhoneNumberExample = true
                phoneTF.placeholder = "Phone Number"
                phoneTF.setFlag(countryCode: .AE)
    }

extension AddNewAddressVC: FPNTextFieldDelegate {

    func fpnDidValidatePhoneNumber(textField: FPNTextField, isValid: Bool) {
        textField.rightViewMode = .whileEditing
        textField.rightView = UIImageView(image: isValid ?  imageLiteral(resourceName: "success") :  imageLiteral(resourceName: "error"))
        print(
            isValid,
            textField.getFormattedPhoneNumber(format: .E164) ?? "E164: nil",
            textField.getFormattedPhoneNumber(format: .International) ?? "International: nil",
            textField.getFormattedPhoneNumber(format: .National) ?? "National: nil",
            textField.getFormattedPhoneNumber(format: .RFC3966) ?? "RFC3966: nil",
            textField.getRawPhoneNumber() ?? "Raw: nil"
        )
    }

    func fpnDidSelectCountry(name: String, dialCode: String, code: String) {
        print(name, dialCode, code)
        self.selectedDialCod = dialCode
    }


    func fpnDisplayCountryList() {
        let navigationViewController = UINavigationController(rootViewController: listController)
        listController.title = "Countries"
        listController.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .stop, target: self, action: #selector(dismissCountries))
        self.present(navigationViewController, animated: true, completion: nil)
    }
}
1 Answers

// You can change the chosen flag then set the phone number

phoneNumberTextField.setFlag(for: .FR)
phoneNumberTextField.set(phoneNumber: "0600000001")

// Or directly set the phone number with country code, which will update automatically the flag image

phoneNumberTextField.set(phoneNumber: "+33600000001")
Related