Skip to content

A iOS application written in Swift, this project is a library for CocoaPods that allows you to validate, generate and manipulate CPF and CNPJ numbers

License

Notifications You must be signed in to change notification settings

diggosilva/CPFCNPJTools

Folders and files

NameName
Last commit message
Last commit date

Latest commit

0f105c6 · Dec 23, 2024

History

86 Commits
Dec 22, 2024
Dec 22, 2024
Dec 22, 2024
Nov 27, 2024
Dec 23, 2024
Nov 16, 2024
Nov 26, 2024
Nov 23, 2024
Dec 19, 2024
Dec 23, 2024
Nov 23, 2024

Repository files navigation

CPFCNPJTools

iOS LinkedIn: @RodrigoSilva

A iOS application written in Swift, this project is a library for CocoaPods that allows you to validate, generate and manipulate CPF and CNPJ numbers in a simple and efficient way. Ideal for testing and filling out forms.

Difference between (CPF), (CNPJ) and (CNPJ Alphanumeric)

  • CPF (Individual Taxpayer Registry): The CPF is an individual registration number used by the Brazilian Federal Revenue Service to identify an individual. The CPF is made up of 11 digits (XXX.XXX.XXX-XX). It is used for various purposes, such as opening bank accounts, issuing invoices, and filing taxes.

  • CNPJ (National Registry of Legal Entities): The CNPJ is a registration number used by the Brazilian Federal Revenue Service to identify companies or other legal entities. The CNPJ is made up of 14 digits (XX.XXX.XXX/0001-XX). It is used to formalize the existence of a company, allowing it to carry out commercial activities, such as issuing invoices, paying taxes, among others.

  • In view of the continuous growth in the number of companies and the imminent exhaustion of available CNPJ numbers, the Brazilian Federal Revenue Service is launching the Alphanumeric CNPJ. This solution aims to facilitate the identification of all companies and improve the business environment, contributing to the economic and social development of Brazil.

  • The Alphanumeric CNPJ will be assigned, starting in July 2026, exclusively to new registrations. Existing CNPJ numbers will not undergo any change, that is, those who are already registered with the CNPJ will keep their number valid!

Contents

Requirements

  • iOS 17.0 or later
  • Xcode 15.0 or later
  • Swift 5.0 or later

Functionalities

  • CPF Validation: Check if a CPF number is valid, following the rules of the Federal Revenue Service.
  • CNPJ Validation: Check if a CNPJ number is valid, according to tax rules.
  • CNPJ Alphanumeric Validation: Check if a CNPJ Alphanumeric is valid, according to tax rules.
  • Fake CPF generation: Generate a valid CPF number randomly.
  • Fake CNPJ generation: Generate a valid CNPJ number randomly.
  • Fake CNPJ Alphanumeric generation: Generate a valid CNPJ Alphanumeric randomly.

Instalation with Cocoapods

Add the dependency to your Podfile:

$ pod 'CPFCNPJTools'

Then run:

$ pod install

Instalation with (SPM) - Swift Package Manager (Xcode 11+)

Swift Package Manager (SPM) is a tool for managing the distribution of Swift code as well as C-family dependency. From Xcode 11, SwiftPM got natively integrated with Xcode.

CPFCNPJTools support SPM from version 5.1.0. To use SPM, you should use Xcode to open your project. Click File -> Add Package Dependencies, enter CPFCNPJTools repo's URL.

HowToUse

CPF

1.1 Validate CPF

  • To validate a CPF, simply use the validate(cpf: String) -> CPFStatus method:
let cpfManager = CPFManager()
let status = cpfManager.validate(cpf: "11144477735")
print(status) // .valid, .invalid, .cpfNull, .equalDigits or .invalidFormat

1.2 Generate Fake CPF masked

  • To generate a fake CPF with mask, simply use the generateMasked() method:
let cpfManager = CPFManager()
let fakeCPFMasked = cpfManager.generateMasked()
print(fakeCPFMasked) // "111.444.777-35"  // Randomly generated CPF

CNPJ

2.1 Validate CNPJ

  • To validate a CNPJ, simply use the validate(cnpj: String) -> CNPJStatus) method:
let cnpjManager = CNPJManager()
let status = cnpjManager.validate(cnpj: "11444777000135")
print(status) // .valid, .invalid, .cnpjNull, .equalDigits or .invalidFormat

2.2 Generate Fake CNPJ masked

let cnpjManager = CNPJManager()
let fakeCNPJMasked = cnpjManager.generateMasked()
print(fakeCNPJMasked) // "11.444.777/0001-35"  // Randomly generated CNPJ

CNPJ Alphanumeric

3.1 Validate CNPJ Dual Format

  • To validate a CNPJ Alphanumeric, simply use the validate(cnpjDualFormat: String) -> CNPJDualFormatStatus method:
let cnpjDualFormatManager = CNPJDualFormatManager()
let status = cnpjDualFormatManager.validate(cnpjDualFormat: "12ABC34501DE35")
print(status)  // .valid, .invalid, .cnpjNull, .equalDigits or .invalidFormat

3.2 Generate Fake CNPJ Dual Format masked

let cnpjDualFormatManager = CNPJDualFormatManager()
let fakeCNPJAlphaNum = cnpjDualFormatManager.generate()
print(fakeCNPJAlphaNum)  // "12.ABC.345/01DE-35"  // Randomly generated CNPJ Alphanumeric

Notes:

  • The file is formatted in Markdown (.md), which is the standard for GitHub readme files.
  • The methods are clearly described, with code examples in Swift to show how to use the library.
  • The explanations and comments are all in English, making the documentation accessible to a wider audience.

Feel free to tweak any parts of the documentation to fit your style or add any additional information that you think is important!

If you'd like any more changes or additions, let me know.

Credits

  • Diggo Silva
  • Helio Mesquita

About

A iOS application written in Swift, this project is a library for CocoaPods that allows you to validate, generate and manipulate CPF and CNPJ numbers

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published