メニュー

Expand
ページを評価:

Twilio Verify Push iOS SDK Quickstart

Verify Push is in Public Beta.

Twilio Verify Push SDK helps you verify users by adding a low-friction, secure, cost-effective, "push verification" factor into your own mobile application. It works by registering your user's iOS devices as a secure key via public-key cryptography. When your app backend needs to verify that the user is who they say they are, you challenge the user to prove that they still possess their secure key (iOS device).

You can run the Verify Push iOS SDK in your existing iOS app and verify a user via push verification using Verify Push API. This Quickstart will walk you through the entire process step-by-step, starting with setting up your Twilio account all the way through verifying a user using your existing iOS app and backend.

このクイックスタートでは、下記のことを学んでいきます:

  1. Twilioにサインアップ
  2. Configure Push Credential and Verify Service
  3. Embed the client SDK into your iOS app
  4. Setup your app backend
  5. Register a user and their device in Verify Push
  6. Configure webhooks
  7. Verify a user

By the end of this Quickstart, you’ll have a solid foundation for implementing Verify Push within your app and backend to verify users at login, transaction, and other sensitive actions.

Want a technical overview first?

Technical Overview

Check out the Verify Push SDK Technical Overview to view its data model, sequence diagrams, and security FAQs.

Want to see an example?

Run Sample App

If you don't want to setup Verify Push with your own app or backend immediately, we have built a Sample App embedded with the client Verify Push iOS SDK and a Sample Node.js Backend that you can run to see Verify Push in action. These samples can also be helpful for troubleshooting.

 

Want to start with Android?

Androidクイックスタート

Add Verify Push to your Android app with the Android SDK quickstart.

 

Already have a Twilio account? Go ahead and skip this section.

You can sign up for a free Twilio trial account here.

  • When you sign up, you'll be asked to verify your personal phone number. This helps Twilio verify your identity.
  • Once you verify your number, you'll be asked to create a project. For the sake of this tutorial, you can click on the "Learn and Explore" template. Give your project a name, or just click "skip remaining steps" to continue with the default.
  • Once you get through the project creation flow, you'll arrive at your project dashboard in the Twilio Console. This is where you'll be able to access your Account SID, an authentication token, create a Push Credential, create a Verify Service and more.

Set up APNs for your iOS app

Already configured push notifications for your iOS app? Go ahead and skip this section.

To configure push notifications, follow these Apple Push Notification service (APNs) setup steps.

Step 1 - Setup an App ID

Log into your Apple development account and click on Identifiers under the Certificates, IDs & Profiles. This will show a list of identifiers, including App IDs.

Option 1- Existing App ID

If your App ID is already on this list, click on it and a list of capabilities will be shown.

  1. Check the 'Push Notifications' capability.
  2. Don't worry about the Configure button right now, just click the Save button in the upper right.

Option 2- New App ID

If your App ID isn't on this list, click the "+" symbol to add a new App ID.

  1. Choose App IDs and click the Continue button in the upper right.
  2. Select App as type
  3. Give your app a description as you see fit.
  4. Give it an Explicit Bundle ID that matches the bundle identifier in Xcode (such as com.twilio.TwilioVerifyDemo).
  5. Under Capabilities check Push Notifications
  6. Click the Continue button
  7. Click the Register button to confirm your new App ID and create it.

Be sure to add Push notifications and Remote notifications capabilities to your Xcode project

Step 2 - Create a Certificate

Now you will need to create a push notification certificate. This will enable your app to receive notifications. You can either make a development certificate or a production certificate. For this guide, we'll make a development certificate.

  1. Add a certificate on the Apple Developer Portal
  2. Under Services, select 'Apple Push Notification service SSL (Sandbox)'.
  3. Click 'Continue'
  4. Select the App ID you previously created.
  5. Click 'Continue' again
  6. It will prompt you to create a Certificate Signing Request, with instructions on how to do it in the 'Learn more' link. Create one.
  7. Upload the Certificate Signing Request.
  8. Click 'Continue' again
  9. Click 'Download' to obtain your certificate.

Congrats! You just created an Apple Development iOS Push Services certificate. Now you can download it and double click on it locally to add it to your Keychain access.

When creating a certificate for push notifications, you can use 'Apple Push Notification service SSL (Sandbox & Production)' if you want to use only one certificate for both environments. The certificate will be labeled as "Apple Push Services".

プッシュクレデンシャル を作成する

For Verify Push to send push notifications to your iOS app, you will need to create an Apple Push Notification (APN) Push Credential that contains your APN certificate and private key.

Get your certificate and private key

In Keychain Access, go to the 'Certificates' Category in the lefthand sidebar.

  1. Right-click your push certificate. It should be labeled "Apple Development iOS Push Services" (or "Apple Push Services").
  2. Choose "Export"
  3. Save it as cred.p12. (Leave the password blank)

This is your credential file. We're going to extract your certificate key and private key from this file. We need these 2 keys to create a Twilio Credential.

Create a Certificate Key

ターミナルで次のコマンドを実行します。

openssl pkcs12 -in cred.p12 -nokeys -out cert.pem -nodes

Create a Private Key

ターミナルで次のコマンドを実行します。

openssl pkcs12 -in cred.p12 -nocerts -out key.pem -nodes

Process the RSA key

ターミナルで次のコマンドを実行します。

openssl rsa -in key.pem -out key.pem

Then you can use the certificate in the cert.pem file and the private key in the key.pem file to create a push credential

Create the push credential

Follow these steps to do it in the Twilio Console.

Screen Shot 2020-10-02 at 11.21.42 AM (1).jpg

  • Click the + button to add a new credential and a form will pop-up:

Screen Shot 2020-10-02 at 11.37.14 AM.jpg

  • Enter a friendly name, such as "Verify Quickstart App Credentials"
  • Select APN push credentials as type
  • Enter the Certificate from the cert.pem
    • strip anything outside of "-----BEGIN CERTIFICATE-----" and "-----END CERTIFICATE-----" boundaries.
  • Enter the Private key from the key.pem.
    • strip anything outside of "-----BEGIN RSA PRIVATE KEY-----" and "-----END RSA PRIVATE KEY-----" boundaries
  • Check the Sandbox option
    • Your app should be signed with a development certificate to use sandbox
  • Click the Create button
  • Note: Although Verify Push uses Twilio Notify for sending push notifications, you do not need to create your own Notify Service

You will need to create/update your push credential for production use, using the certificate and private key for a 'Apple Push Notification service SSL (Sandbox & Production)' certificate and the Sandbox checkbox should be unchecked. Your app should be signed with a production certificate.

Create a Verify Service and add the Push Credential

Screen Shot 2020-10-02 at 11.19.35 AM (1).jpg

  • Alternatively, you can select any existing Service. However, we recommend creating a new service for testing so you don't accidentally interfere with a production environment.
  • Go to the Service's Settings and select the Credential SID(s) that you want this Service to use for sending push notifications. One SID can be selected for your Android app (FCM) and one for your iOS app (APN). For web apps, select the FCM credential SID:

Screen Shot 2020-10-02 at 11.56.48 AM (1).jpg

Embed the SDK into your iOS app

Follow the installation steps in the README file of the Twilio Verify iOS repo. The installation steps will explain how to add the iOS library to your project and setup your iOS app to work with Apple Push Notification service (APNs).

After you import the Twilio Verify iOS library, you can start to integrate your existing app with Verify Push.

To get started, you will need to create an instance of the TwilioVerify class, using its Builder. Do this by typing or pasting in the code sample.

        
        
        

        The SDK should be used from a Swift class.

        Full source

        Obtain a Verify Push API access token for your iOS app

        Your iOS app needs to obtain an Access Token (technically an Enrollment JWE, similar to a JSON Web Token (JWT)) to make direct requests to the Verify Push API in the future. This Access Token request needs to be brokered by your app backend. To do this, you need to:

        1. Expose an API endpoint in your app backend for your iOS app to request the Access Token

        2. Have your app backend request an Access Token from Verify Push API

        3. Return the Access Token, identity and Verify Service SID to the iOS app

        Expose an API endpoint in your app backend for your app to request the Access Token

        Check out our Sample Node.js Backend for an implementation example. In a production web application, you would restrict access token retrieval to the currently authenticated user, using whatever type of authentication your service already uses.

        Have your app backend request an Access Token from Verify Push API

        In the sidebar to the right (or below) is sample code for requesting an access token from the Verify Push API in all languages supported by Twilio helper libraries. You will need the Verify service_sid, as well as the user id (such as a GUID or the user's primary key in your database table) you use as the identity with this Verify service.

              
              
              
              Your app backend should make this request

              Request an Access Token from Verify Push API

              Your app backend should make this request

              Do not use Personally Identifiable Information for identity. Use an immutable user identifier like a UUID, GUID, or SID.
              Verify Push uses identity as a unique identifier of a user. You should not use directly identifying information (aka personally identifiable information or PII) like a person's name, home address, email or phone number, etc., as identity because the systems that will process this attribute assume it is not directly identifying information.

              Register a user and their device

              Do not store your Verify Service SID in your app. Get it from your backend, as well as the identity. You can find an example in the sample backend

              Register a user and their device in Verify Push by creating and verifying a Factor. Type or paste the code samples for both of these steps.

              Create a factor

              You don’t need to make a separate Create Entity call, because the Create Factor call will create an Entity and a Factor at the same time.

              As getting the device token for push could happen in a different moment before creating the factor, please validate that your app has a device token before using it as a pushToken in the create factor method.

                    
                    
                    

                    Full source

                    Verify a factor

                          
                          
                          

                          Full source

                          Configure webhooks (optional)

                          Configure a webhook callback for your app backend to be notified of events such as when a Factor has been verified or when a Challenge has been approved, so that it knows to advance the user to the next step in your flow. This is more real-time and efficient than constantly polling the Verify Push API for the status of a Factor or Challenge.

                          Follow the steps in this Verify Webhooks page.

                                
                                
                                
                                Specify events to send to your callback URL

                                Configure webhook

                                Specify events to send to your callback URL

                                Verify a user

                                Congratulations! Verify Push consists of two user sequences, and you've just completed the first one: user and device registration. The second sequence is to challenge and verify (authenticate) a user with their registered device. Read on for the step-by-step instructions.

                                Create a Challenge

                                Your web application backend needs to call the Verify Push API to create a Challenge using the Factor that you've just created. When the challenge is created, Verify Push sends a push notification to the registered device using the configured Push Credential.

                                The code in the sidebar to the right/bottom demonstrates how to create those challenges using the Twilio helper libraries.

                                      
                                      
                                      

                                      You may choose to put Personally Identifiable Information (PII) or other sensitive information in details and hidden_details. The data in each param will be stored per its retention policy listed in the Challenge resource.

                                      Read Push Notification

                                      Your iOS app needs to read the contents of the push notification payload to obtain the challengeSid, factorSid, and message parameters.

                                      Typically, you would do this either directly in the AppDelegate class, or you would create a new class that handled incoming push notifications that you reference in your AppDelegate class.

                                      The following code sample runs when there is an incoming push notification, and if the type is verify_push_challenge, reads the parameters out of the notification.

                                      Because the user might consider notification-based interactions disruptive, you must obtain permission to use them.

                                            
                                            
                                            

                                            Testing push notifications on simulators

                                            You can test push notifications on simulators. The Xcode command-line tools allow you to send push notifications from the terminal, this command allows you to send a push notification to the iOS simulator:

                                            xcrun simctl push --help
                                            Send a simulated push notification
                                            Usage: simctl push <device> [<bundle identifier>] (<json file> | -)
                                            
                                            	bundle identifier
                                            	     The bundle identifier of the target application
                                            	     If the payload file contains a 'Simulator Target Bundle' top-level key this parameter may be omitted.
                                            	     If both are provided this argument will override the value from the payload.
                                            	json file
                                            	     Path to a JSON payload or '-' to read from stdin. The payload must:
                                            	       - Contain an object at the top level.
                                            	       - Contain an 'aps' key with valid Apple Push Notification values.
                                            	       - Be 4096 bytes or less.
                                            
                                            Only application remote push notifications are supported. VoIP, Complication, File Provider, and other types are not supported.

                                            The command requires the arguments:

                                            • <device>: set this to booted to use the open simulator. You can also use a device identifier which you could get with the xcrun simctl list devices | grep Booted command.
                                            • <bundle identifier>: bundle identifier of the app you are testing.
                                            • <json file>: a JSON file containing the push notification details.

                                            The JSON content for getting a challenge push notification is:

                                            {
                                                "aps": {
                                                    "alert": "<Challenge message>",
                                                    "category": "verify_push_challenge"
                                                },
                                                "message": "<Challenge message>",
                                                "challenge_sid": "<challenge_sid>",
                                                "factor_sid": "<factor_sid>",
                                                "type": "verify_push_challenge"
                                            }
                                            • <Challenge message>: The message for the challenge
                                            • <factor_sid>: A valid factor Sid, created in the device
                                            • <challenge_sid>: A valid challenge Sid, created for the factor

                                            Running this command for the sample app will be:

                                            xcrun simctl push booted com.twilio.TwilioVerifyDemo payload.json

                                            You can see an example here

                                            Get and display Challenge details with your app

                                            Once your app receives the push notification containing the challengeSid, it needs to retrieve the challenge details that correspond to that sid. Type or paste the sample code.

                                                  
                                                  
                                                  

                                                  Full source

                                                  Update the Challenge to verify the user

                                                  In an actual implementation, your iOS app should either display the Challenge details to the user and request confirmation that the Challenge is expected, or silently approve the Challenge, because your app already knows that the user is trying to login on the same device as the registered device that is being challenged.

                                                  In either case, the next step is for your app to call the Verify Push API directly and update the Challenge status as approved or denied. Type or paste the sample code into your iOS app.

                                                        
                                                        
                                                        

                                                        Full source

                                                        Receive Challenge status

                                                        Once Verify Push API receives a Challenge update from your iOS app, it will forward the update by sending a webhook callback (challenge.approved or challenge.denied) to your app backend, so that it knows to take your user to the next step in your flow. If you don't have a webhook configured, you can poll the Verify Push API for the status of the Challenge.

                                                        This completes the second user sequence of Verify Push: Challenge and verify a user.

                                                        おめでとうございます!

                                                        関連トピック

                                                        Now that you've verified your first user, check out the following resources to continue your Verify Push journey with Twilio:

                                                        皆さまが何を開発されるのか、目にするのが待ちきれません!

                                                        ページを評価:

                                                        ヘルプが必要ですか?

                                                        We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Community Forums or browsing the Twilio tag on Stack Overflow.

                                                              
                                                              
                                                              

                                                              フィードバックくださりありがとうございます!

                                                              We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

                                                              Sending your feedback...
                                                              🎉 Thank you for your feedback!
                                                              Something went wrong. Please try again.

                                                              Thanks for your feedback!

                                                              Refer us and get $10 in 3 simple steps!

                                                              ステップ1

                                                              Get link

                                                              Get a free personal referral link here

                                                              ステップ2:

                                                              Give $10

                                                              Your user signs up and upgrade using link

                                                              ステップ3

                                                              Get $10

                                                              1,250 free SMSes
                                                              OR 1,000 free voice mins
                                                              OR 12,000 chats
                                                              OR more