Overview
In this guide you learn how to add the Client SDK to your iOS app.
Prerequisites
To use the Vonage SDK for iOS, you need to have the following installed:
- Xcode 10 or later
- iOS 10.2 or later
Add the SDK to your iOS Project
Open Xcode with your iOS project.
You can either install the Client SDK directly, or via CocoaPods.
CocoaPods
Open your project's
PodFile. If you don't have one already, open a terminal and run the following commands:$ cd 'Project Dir' $ pod initWhere
Project Diris the path to the parent directory of thePodFile.Under your target add the
NexmoClientpod. ReplaceTargetNamewith your actual target name.target 'TargetName' do pod 'NexmoClient' endMake sure the pod file has the public CocoaPod specs repository source.
Install the Pod by opening a terminal and running the following command:
$ cd 'Project Dir' $ pod updateWhere
Project Diris the path to the parent directory of thePodFile.Open the
xcworkspacewith Xcode and disablebitcodefor your target.In your code, import the
NexmoClientlibrary:
import NexmoClient
#import <NexmoClient/NexmoClient.h>;
Add permissions
To use the in-app voice features, you need to add audio permissions:
In your
Info.plistadd a new row with 'Privacy - Microphone Usage Description' and a description for using the microphone. For example,Audio Calls.In your code add a request for Audio Permissions:
import AVFoundation
func askAudioPermissions() {
AVAudioSession.sharedInstance().requestRecordPermission { (granted:Bool) in
NSLog("Allow microphone use. Response: %d", granted)
}
}
#import <AVFoundation/AVAudioSession.h>
- (void)askAudioPermissions {
if ([[AVAudioSession sharedInstance] respondsToSelector:@selector(requestRecordPermission:)])
{
[[AVAudioSession sharedInstance] requestRecordPermission: ^ (BOOL granted)
{
NSLog(@"Allow microphone use. Response: %d", granted);
}];
}
}
AppDelegate is the best place to do this.
Using client in your app
Login
Create a NXMClient object and login with a jwt user token. If necessary, you can read more about generating the JWT.
let client = NXMClient.shared
client.setDelegate(self)
client.login(withAuthToken: "your token")
NXMClient *client = [NXMClient shared];
[self.client setDelegate:self];
[self.client loginWithAuthToken:@"your token"];
Note that `self` should implement the `NXMClientDelegate` protocol.
Connection status
On a successful login, the following delegate method is called with NXMConnectionStatusConnected:
func client(_ client: NXMClient, didChange status: NXMConnectionStatus, reason: NXMConnectionStatusReason)
- (void)client:(nonnull NXMClient *)client didChangeConnectionStatus:(NXMConnectionStatus)status reason:(NXMConnectionStatusReason)reason
You can check if the connection status is connected with isConnected:
var clientIsConnected: Bool = NXMClient.shared.isConnected()
BOOL clientIsConnected = [NXMClient.shared isConnected];
isConnected will be the last connection status of the client if your app is suspended in the background. Therefore the value for isConnected can be stale.
Get current user info
After the login succeeds, the logged in user will be available via:
let user = client.user
NXMUser *user = client.user;
Conclusion
You added the Client SDK to your iOS app, and logged in to a NXMClient instance. You can now use the NXMClient client in your app, and use the Client SDK functionality.
See also
- Data Center Configuration - this is an advanced optional configuration you can carry out after adding the SDK to your application.