Free SMS OTP API for Swift iOS App

PhoneEmail @ https://phone.email
3 min readFeb 15, 2024

--

Enhance User Authentication with Free OTP SMS API for Swift iOS Apps

Introduction:

In the fast-evolving landscape of mobile app development, user authentication is a crucial aspect. Innovations like Phone.Email’s OTP API have emerged to streamline and enhance the user verification process. One standout feature is the “Log in with phone” button, offering a cost-effective solution for phone number verification. This blog will explore the key features and integration steps for utilizing this Free OTP SMS API in Swift iOS apps.

Key Features of Phone.Email’s OTP API:

Before delving into the integration process, let’s highlight some key features that make Phone.Email’s OTP API a standout solution:

  1. Cost-Effective Phone Verification: Phone.Email provides a solution for verifying phone numbers without incurring significant costs. The “Log in with phone” button initiates the verification process at minimal or no expense.
  2. Secure Authentication: The OTP API ensures a secure authentication process. Upon clicking the “Log in with phone” button, users are prompted to enter their country code and mobile number. A one-time password (OTP) is then sent to the user’s mobile device, adding an extra layer of security.
  3. JWT Auth Token Integration: Once the user is successfully verified, the control redirects to the client website page with a JSON Web Token (JWT) authentication token. This token can be utilized for subsequent secure interactions with the server.

Integration Steps:

Now, let’s walk through the integration steps to add the “Log in with phone” button and leverage the Free OTP SMS API in a Swift iOS app.

  1. Create Admin Account:

To get started, create an admin account on Phone.Email’s admin dashboard Admin Dashboard. This account will provide you with the necessary credentials and access to configure the “Log in with phone” button.

2. Add “Login with Phone” Button:

Integrating the “Log in with phone” button is straightforward. Below is a Swift code snippet that demonstrates how to add the button and initiate the authentication process:

//Add login button     
@IBOutlet weak var phoneButton: UIButton!

override func viewDidLoad() {
super.viewDidLoad()
self.phoneButton.addShadow()
self.container.addShadow()

// Additional setup code

}

On click of the button, open the authentication URL in a webview . Replace placeholders with actual phone country code and number.

let urlString = "https://auth.phone.email/sign-in?countrycode=\(phoneCountry)&phone_no=\(phoneNumber)&auth_type=7&device=\(deviceId)"

if let url = URL(string: urlString) {
let request = URLRequest(url: url)
webView.load(request)
}

Check out the complete Swift code example on GitHub.

3. Get Verified Phone Numbers Back:

After the user completes the authentication process, you can retrieve verified phone numbers using the following Swift code:

func presentAuthViewController() {
// Instantiate AuthViewController from storyboard
let authViewController = self.storyboard?.instantiateViewController(withIdentifier: "AuthViewController") as! AuthViewController

// Callback function to handle the result data and extract JWT
authViewController.didReceiveJWT = { jwt in
if let jwt = jwt {
// Handle the JWT and extract relevant information
let decodedClaims = JWTDecoder.decodeJWT(jwt)

if let phone = decodedClaims.phoneNo, !phone.isEmpty {
// Process the verified phone details
self.showDetails(verifiedPhoneDetail: "\(decodedClaims.countryCode ?? "") \(decodedClaims.phoneNo ?? "")", source: 2)
self.getEmailCount(jwt: jwt)
} else {
self.showAlert(message: "Something went wrong!!")
}
} else {
self.showAlert(message: "Something went wrong!!")
}
}

// Present the authentication view controller
present(authViewController, animated: true, completion: nil)
}

For a comprehensive guide and documentation, refer to Phone.Email’s Docs.

Conclusion:

Incorporating the “Log in with phone” button powered by Phone.Email’s OTP API into your Swift iOS app not only simplifies the user authentication process but also adds an extra layer of security. With cost-effective phone verification and seamless integration, this solution is a valuable addition to any app requiring robust user verification.

--

--

PhoneEmail @ https://phone.email

Phone Email is a phone first business, delivering phone authentication and email solution.