简体   繁体   中英

IOS: How to verify user's phone number using codes

i am working on an app in which i take a field of mobile number of user after entering mobile number there is a option for verify phone number. but i don't know the functionality of how to verify a user's phone number within the app using codes.I search for similar type of questions but didn't get the exact solutions. i am sending a screenshot of my app where i want to put that functionality. i didn't apply any codes for this
please help

在此处输入图片说明

You need a backend that can generate and save codes for each phone number and also send a text message with those codes. So the flow is as follows:

  1. User enters their phone number in the app and presses the "Verify Number" button.
  2. You send a request to your backend with the number provided. In the app, you also displays a text field for user to enter the generated code and a new button like "Check code".
  3. The backend generates a new code (just a random one), stores it to the database (like ID,PhoneNumber,GeneratedCode,DateOfGeneration) and sends a text message with the code to the phone number specified.
  4. When the user receives the message he or she enters the code to the checking input field and presses the "Check" button.
  5. The mobile app sends another request to the backend with the phone number entered earlier and the code entered by the user now.
  6. The backend looks to the database for the "Phone number" - "Code" pair and responds with failure (the code is incorrect, try again) or success (the phone is verified).

Unfortunately, there's no way to access text messages received by user from within an app on iOS devices (unlike Android). The proof link was already provided. So the user has to manually enter the verification code from the message.

Ok I tell the information how was I already did my project in before

Step-1

when user press the Verify Number button

  • initially I check the phone number is valid or not(means phone number count/length).

  • second I generate the random number on progrmatically like NSUInteger r = arc4random_uniform(16);

    Step-2

    send the random number to server along with the vaild phone number , the server send the random number to the particular mobile number using SMTP Server.

    Step-3

    in your hand you have the random number , so open the UIAlertview for user type the valid random number , if user typed the vaild number show the Next Screen else show the Alert .

To verify any mobile number you can use third party api's which are available for mobile as well as backend server. You can use Twilio Messaging Api . To use this API follow below steps:

  1. Register account on Twilio and get key from account.
  2. Get user's phone number from mobile application.
  3. When user click on the "Verify Number" button, call web-service with the number.
  4. When you'll call the web-service, write a logic to send the random number with the Twilio messaging api to send message to user.
  5. When user get this number through SMS, You can tell user to enter the number and verify it.

Also, there is another api which you can directly integrate into your mobile application. You can find this Sinch Api here.

To verify against your device's phone number, first, you need to get the phone number from your device and then you can compare against. However, after iOS 4, getting phone number of your device is quite impossible. Even if, you do get the device's number using some private api which I am not sure works entirely, there is a huge possibility for Apple to reject your app.

Refer to the following stack overflow discussion- Programmatically get own phone number in iOS

Just a suggestion: You probably want to look for a work around based on your business goal. For example, rather than checking for device's phone number directly. You can generate a code and send that to the number specified by the user and then enter that code to verify that the user has that device.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM