how to send verification code by sms in swift 2

只愿长相守 提交于 2020-01-04 06:38:05

问题


i build a register form for my app and i need to send the user a verifiation code by sms in order to complete the registration proccess.

i tried to use MFMessageComposeViewController but its open the dialog sms on the device so the user can see the code.

i also checked the web for 3party of sending sms but there is a problem with the country code. i know its posible becuse whatsapp do it to confirm the user.

what it the right way to do it?

this is the topic the i tried: Sending SMS in iOS with Swift


回答1:


The best way to achieve this is by creating some views for allowing the user to enter the phone number with the country code which can be used by a server to send a request for initiating the OTP verification. To achieve this you need to:

  1. Create View Controllers.
  2. Upload Phone Number and Country code to the server.
  3. Validate the requests by verifying the OTP.

As mentioned by Dan, you can use Digits in Fabric for that purpose, and create custom views for a great UX.

On the other hand, you can also use a service called as SendOTP from MSG91 - you can use it for internal testing and development ideas as they provide you with 5,000 free OTP SMS. The service has a complete set of APIs which you can implement on the backend as well on the app front. Also, they provide a framework so that you don't need to create the views, but only presentViewController and call delegate methods for knowing what happened during the verification process - such as Cancelled or Verified or Failed.

Swift implementation of the same looks like this:

    class OTPFrame: UIViewController, sendOTPAuthenticationViewControllerDelegate {    

   func loadOTPFramework() {
            SendOTP.sharedManager().startWithApiId("yourAppID")
            let frameworkPath: NSString = NSBundle.mainBundle().privateFrameworksPath!
            let frameworkBundlePath: NSString = frameworkPath.stringByAppendingPathComponent("SendOTPFramework.framework")
            let frameworkBundle : NSBundle
                = NSBundle(path: frameworkBundlePath as String)!
            let authenticationViewController: AuthenticationViewController = AuthenticationViewController(nibName: "AuthenticationViewController", bundle: frameworkBundle)
            authenticationViewController.delegate = self                self.presentViewController(authenticationViewController, animated: true, completion: nil)
        }

        func authenticationisSuccessfulForMobileNumber(mobNo: String!, withCountryCode countryCode: String!) {
            print("Success")
        }

        func canceledAuthentication() {
            print("Cancelled")
        }

        func authenticationisFailedForMobileNumber(mobNo: String!, withCountryCode countryCode: String!) {
            print("Failed")
        }
    }

Disclaimer: I, in no way, endorse the services mentioned above - you are free to choose whatever you like.

Thank You!




回答2:


I would give digits a try! It's part of the Twitter Fabric package and it's very simple to use. The user enters their phone number and Fabric takes care of validating the number.



来源:https://stackoverflow.com/questions/36518159/how-to-send-verification-code-by-sms-in-swift-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!