TwilioVoice Class Reference
| Inherits from | NSObject |
|---|---|
| Declared in | TwilioVoice.h |
Overview
TwilioVoice is the entry point to the Twilio Voice SDK. You can register for VoIP push notifications, make outgoing
Calls, receive Call invites and set audio device to use in a Call using this class.
CallKit Audio Session Handling
Other Methods
logLevel
The current logging level used by the SDK.
@property (nonatomic, assign, class) TVOLogLevel logLevelDiscussion
The default logging level is TVOLogLevelError. TwilioVoice and its components use NSLog internally.
See Also
Declared In
TwilioVoice.h
audioDevice
The TVOAudioDevice used to record and playback audio in a Call.
@property (class, nonatomic, strong, nonnull) id<TVOAudioDevice> audioDeviceDiscussion
If you wish to provide your own TVOAudioDevice then you must set it before performing any other
actions with the SDK like connecting to a Call. It is also possible to change the device when you are no longer
connected to any Calls and have destroyed all other SDK objects.
See Also
Declared In
TwilioVoice.h
+ sdkVersion
Returns the version of the SDK.
+ (nonnull NSString *)sdkVersionReturn Value
The version of the SDK.
Declared In
TwilioVoice.h
+ setLogLevel:module:
Sets the logging level for an individual module.
+ (void)setLogLevel:(TVOLogLevel)logLevel module:(TVOLogModule)moduleParameters
logLevel |
The |
|---|---|
module |
The |
See Also
Declared In
TwilioVoice.h
+ logLevelForModule:
Retrieve the log level for a specific module in the TwilioVoice SDK.
+ (TVOLogLevel)logLevelForModule:(TVOLogModule)moduleParameters
module |
The |
|---|
Return Value
The current log level for the specified module.
Declared In
TwilioVoice.h
Managing VoIP Push Notifications
+ registerWithAccessToken:deviceToken:completion:
Registers for Twilio VoIP push notifications.
+ (void)registerWithAccessToken:(nonnull NSString *)accessToken deviceToken:(nonnull NSString *)deviceToken completion:(nullable void ( ^ ) ( NSError *__nullable error ))completionParameters
accessToken |
Twilio Access Token. |
|---|---|
deviceToken |
The push registry token for Apple VoIP Service. |
completion |
Callback block to receive the result of the registration. |
Discussion
Registering for push notifications is required to receive incoming call notification messages through Twilio’s infrastructure. Once successfully registered, the registered binding has a time-to-live(TTL) of 1 year. If the registered binding is inactive for 1 year it is deleted and push notifications to the registered identity will not succeed. However, whenever the registered binding is used to reach the registered identity the TTL is reset to 1 year. A successful registration will ensure that push notifications will arrive via the APNs for the lifetime of the registration device token provided by the APNs instance.
The identity provided in the Access Token may only contain alpha-numeric and underscore characters. Other characters, including spaces, will result in undefined behavior.
If the registration is successful the completion handler will contain a Null NSError. If the registration
fails, the completion handler will have a nonnull NSError with UserInfo string describing the reason for failure.
| Registration Error | Error Code | Description |
| TVOErrorAccessTokenInvalidError | 20101 | Twilio was unable to validate your Access Token |
| TVOErrorAccessTokenHeaderInvalidError | 20102 | Invalid Access Token header |
| TVOErrorAccessTokenIssuerInvalidError | 20103 | Invalid Access Token issuer or subject |
| TVOErrorAccessTokenExpiredError | 20104 | Access Token has expired or expiration date is invalid |
| TVOErrorAccessTokenNotYetValidError | 20105 | Access Token not yet valid |
| TVOErrorAccessTokenGrantsInvalidError | 20106 | Invalid Access Token grants |
| TVOErrorExpirationTimeExceedsMaxTimeAllowedError | 20157 | Expiration Time in the Access Token Exceeds Maximum Time Allowed |
| TVOErrorAccessForbiddenError | 20403 | Forbidden. The account lacks permission to access the Twilio API |
| TVOErrorTokenAuthenticationRejected | 51007 | Token authentication is rejected by authentication service |
| TVOErrorRegistrationError | 31301 | Registration failed. Look at [error localizedFailureReason] for details |
Insights
| Group Name | Event Name | Description | Since version |
| registration | registration | Registration is successful | 3.0.0-beta3 |
| registration | registration-error | Registration failed | 3.0.0-beta3 |
An example of using the registration API
[TwilioVoice registerWithAccessToken:accessToken
deviceToken:deviceToken
completion:^(NSError *error) {
if(error != nil) {
NSLog("Failed to register for incoming push: %@\n\t - reason: %@", [error localizedDescription], [error localizedFailureReason]);
} else {
NSLog("Registration successful");
}
}];
Declared In
TwilioVoice.h
+ unregisterWithAccessToken:deviceToken:completion:
Unregisters from Twilio VoIP push notifications.
+ (void)unregisterWithAccessToken:(nonnull NSString *)accessToken deviceToken:(nonnull NSString *)deviceToken completion:(nullable void ( ^ ) ( NSError *__nullable error ))completionParameters
accessToken |
Twilio Access Token. |
|---|---|
deviceToken |
The push registry token for Apple VoIP Service. |
completion |
Callback block to receive the result of the unregistration. |
Discussion
Call this method when you no longer want to receive push notifications for incoming Calls.
The identity provided in the Access Token may only contain alpha-numeric and underscore characters. Other characters, including spaces, will result in undefined behavior.
If the unregistration is successful the completion handler will contain a Null NSError. If the unregistration
fails, the completion handler will have a nonnull NSError with UserInfo string describing the reason for failure.
| Registration Error | Error Code | Description |
| TVOErrorAccessTokenInvalidError | 20101 | Twilio was unable to validate your Access Token |
| TVOErrorAccessTokenHeaderInvalidError | 20102 | Invalid Access Token header |
| TVOErrorAccessTokenIssuerInvalidError | 20103 | Invalid Access Token issuer or subject |
| TVOErrorAccessTokenExpiredError | 20104 | Access Token has expired or expiration date is invalid |
| TVOErrorAccessTokenNotYetValidError | 20105 | Access Token not yet valid |
| TVOErrorAccessTokenGrantsInvalidError | 20106 | Invalid Access Token grants |
| TVOErrorExpirationTimeExceedsMaxTimeAllowedError | 20157 | Expiration Time in the Access Token Exceeds Maximum Time Allowed |
| TVOErrorAccessForbiddenError | 20403 | Forbidden. The account lacks permission to access the Twilio API |
| TVOErrorTokenAuthenticationRejected | 51007 | Token authentication is rejected by authentication service |
| TVOErrorRegistrationError | 31301 | Registration failed. Look at [error localizedFailureReason] for details |
Insights
| Group Name | Event Name | Description | Since version |
| registration | unregistration | Unregistration is successful | 3.0.0-beta3 |
| registration | unregistration-error | Unregistration failed | 3.0.0-beta3 |
An example of using the unregistration API
[TwilioVoice unregisterWithAccessToken:accessToken
deviceToken:deviceToken
completion:^(NSError *error) {
if(error != nil) {
NSLog("Failed to unregister for incoming push: %@\n\t - reason: %@", [error localizedDescription], [error localizedFailureReason]);
} else {
NSLog("Unregistration successful");
}
}];
Declared In
TwilioVoice.h
+ handleNotification:delegate:
Processes an incoming VoIP push notification payload.
+ (BOOL)handleNotification:(nonnull NSDictionary *)payload delegate:(nonnull id<TVONotificationDelegate>)delegateParameters
payload |
Push notification payload. |
|---|---|
delegate |
A |
Return Value
A BOOL value that indicates whether the payload is a valid notification sent by Twilio.
Discussion
This method will synchronously process call notification payload and call the provided delegate on the same dispatch queue.
Twilio sends two types of notifications via Apple VoIP Service, call and cancel.
The notification type is encoded in the dictionary with the key twi_message_type and the values
twilio.voice.call and twilio.voice.cancel.
A call notification is sent when someone wants to reach the registered identity.
Passing a call notification into this method will result in a callInviteReceived: callback with a TVOCallInvite
object and return YES.
A cancel notification is sent to an identity for the following reasons:
- call made to this
identityis prematurelydisconnectedby the caller - call is
rejected - call is
ignored - call is
accepteddue to an outstanding infrastructure issue.
When a call is accepted, a cancel notification is sent to all the devices that have been registered with the identity
accepting the call, even to the device that accepted the call. It is recommended that in this case the developer checks
whether the call sid of TVOCancelledCallInvite matches the accepted call, and if so disregard it.
Passing a cancel notification into this method will result in a cancelledCallInviteReceived: callback with
a TVOCancelledCallInvite object and return YES.
An example of using the handleNotification API
@interface CallViewController () <PKPushRegistryDelegate, TVONotificationDelegate, TVOCallDelegate, ... >
// property declarations
@end
@implementation CallViewController
// ViewController setup and other code ...
#pragma mark - PKPushRegistryDelegate
- (void)pushRegistry:(PKPushRegistry *)registry
didReceiveIncomingPushWithPayload:(PKPushPayload *)payload
forType:(NSString *)type {
NSLog(@"Received incoming push: %@", payload.dictionaryPayload);
if ([type isEqualToString:PKPushTypeVoIP]) {
if (![TwilioVoice handleNotification:payload.dictionaryPayload delegate:self]) {
NSLog(@"The push notification was not a Twilio Voice push notification");
}
}
}
#pragma mark - TVONotificationDelegate methods
- (void)callInviteReceived:(TVOCallInvite *)callInvite {
// handle call invite
}
- (void)cancelledCallInviteReceived:(TVOCancelledCallInvite *)cancelledCallInvite {
// handle cancelled call invite
}
@end
See Also
Declared In
TwilioVoice.h
+ connectWithAccessToken:delegate:
Make an outgoing Call.
+ (nonnull TVOCall *)connectWithAccessToken:(nonnull NSString *)accessToken delegate:(nonnull id<TVOCallDelegate>)delegateParameters
accessToken |
The access token. |
|---|---|
delegate |
A |
Return Value
A TVOCall object.
Discussion
This method is guaranteed to return a TVOCall object. It’s possible for the returned Call to either
succeed or fail to connect.
The identity provided in the Access Token may only contain alpha-numeric and underscore characters. Other characters, including spaces, will result in undefined behavior.
See Also
Declared In
TwilioVoice.h
+ connectWithOptions:delegate:
Make an outgoing Call.
+ (nonnull TVOCall *)connectWithOptions:(nonnull TVOConnectOptions *)options delegate:(nonnull id<TVOCallDelegate>)delegateParameters
options |
The connect options. |
|---|---|
delegate |
A |
Return Value
A TVOCall object.
Discussion
This method is guaranteed to return a TVOCall object. It’s possible for the returned Call to either
succeed or fail to connect. Use the TVOConnectOptions builder to specify Call parameters and UUID.
See Also
Declared In
TwilioVoice.h