Chat API Objective-C SDK for WhatsApp API

Download SDK

You can customize this SDK to your needs and make it more convenient by clicking here:

WhatsApp Objective-C SDK Generator

  • API version: 1.0.0
  • Package version:
  • Build package: org.openapitools.codegen.languages.ObjcClientCodegen

Requirements

The SDK requires ARC (Automatic Reference Counting) to be enabled in the Xcode project.

Installation & Usage

Install from Github using CocoaPods

Add the following to the Podfile:

pod 'OpenAPIClient', :git => 'https:////.git'

To specify a particular branch, append , :branch => 'branch-name-here'

To specify a particular commit, append , :commit => '11aa22'

Install from local path using CocoaPods

Put the SDK under your project folder (e.g. /path/to/objc_project/Vendor/OpenAPIClient) and then add the following to the Podfile:

pod 'OpenAPIClient', :path => 'Vendor/OpenAPIClient'

Usage

Import the following:

#import <OpenAPIClient/OAIApiClient.h>
#import <OpenAPIClient/OAIDefaultConfiguration.h>
// load models
#import <OpenAPIClient/OAIAck.h>
#import <OpenAPIClient/OAIBanSettings.h>
#import <OpenAPIClient/OAIBanTestAction.h>
#import <OpenAPIClient/OAIBanTestStatus.h>
#import <OpenAPIClient/OAIChat.h>
#import <OpenAPIClient/OAIChatIdProp.h>
#import <OpenAPIClient/OAIChatUpdate.h>
#import <OpenAPIClient/OAIChats.h>
#import <OpenAPIClient/OAIClearActionsQueueStatus.h>
#import <OpenAPIClient/OAIClearMessagesQueueStatus.h>
#import <OpenAPIClient/OAICreateGroupAction.h>
#import <OpenAPIClient/OAICreateGroupStatus.h>
#import <OpenAPIClient/OAIForwardMessageRequest.h>
#import <OpenAPIClient/OAIGroupParticipantAction.h>
#import <OpenAPIClient/OAIGroupParticipantStatus.h>
#import <OpenAPIClient/OAIInlineResponse200.h>
#import <OpenAPIClient/OAIInlineResponse2001.h>
#import <OpenAPIClient/OAIInlineResponse2002.h>
#import <OpenAPIClient/OAIInlineResponse2003.h>
#import <OpenAPIClient/OAIInlineResponse2004.h>
#import <OpenAPIClient/OAIInlineResponse2005.h>
#import <OpenAPIClient/OAIInlineResponse2005Update.h>
#import <OpenAPIClient/OAIInlineResponse401.h>
#import <OpenAPIClient/OAIInstanceStatus.h>
#import <OpenAPIClient/OAIInstanceStatusAction.h>
#import <OpenAPIClient/OAIInstanceStatusLink.h>
#import <OpenAPIClient/OAIInstanceStatusStatusData.h>
#import <OpenAPIClient/OAIInstanceStatusStatusDataActions.h>
#import <OpenAPIClient/OAIMessage.h>
#import <OpenAPIClient/OAIMessages.h>
#import <OpenAPIClient/OAIOutboundAction.h>
#import <OpenAPIClient/OAIOutboundActions.h>
#import <OpenAPIClient/OAIOutboundMessage.h>
#import <OpenAPIClient/OAIOutboundMessages.h>
#import <OpenAPIClient/OAIPhoneProp.h>
#import <OpenAPIClient/OAIReadChatAction.h>
#import <OpenAPIClient/OAIReadChatStatus.h>
#import <OpenAPIClient/OAISendContactRequest.h>
#import <OpenAPIClient/OAISendFileRequest.h>
#import <OpenAPIClient/OAISendLinkRequest.h>
#import <OpenAPIClient/OAISendLocationRequest.h>
#import <OpenAPIClient/OAISendMessageRequest.h>
#import <OpenAPIClient/OAISendMessageStatus.h>
#import <OpenAPIClient/OAISendPTTRequest.h>
#import <OpenAPIClient/OAISendVCardRequest.h>
#import <OpenAPIClient/OAISetWebhookStatus.h>
#import <OpenAPIClient/OAISettings.h>
#import <OpenAPIClient/OAIStatus.h>
#import <OpenAPIClient/OAIStatuses.h>
#import <OpenAPIClient/OAIWebhookStatus.h>
#import <OpenAPIClient/OAIWebhookUrl.h>
// load API classes for accessing endpoints
#import <OpenAPIClient/OAIClass1InstanceApi.h>
#import <OpenAPIClient/OAIClass2MessagesApi.h>
#import <OpenAPIClient/OAIClass3ChatsApi.h>
#import <OpenAPIClient/OAIClass4WebhooksApi.h>
#import <OpenAPIClient/OAIClass5QueuesApi.h>
#import <OpenAPIClient/OAIClass6BanApi.h>
#import <OpenAPIClient/OAIClass7TestingApi.h>

Recommendation

It's recommended to create an instance of ApiClient per thread in a multi-threaded environment to avoid any potential issues.

Getting Started

Please follow the installation procedure and then run the following:

OAIDefaultConfiguration *apiConfig = [OAIDefaultConfiguration sharedConfig];

// Configure API key authorization: (authentication scheme: instanceId)
[apiConfig setApiKey:@"YOUR_API_KEY" forApiKeyIdentifier:@"instanceId"];
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
//[apiConfig setApiKeyPrefix:@"Bearer" forApiKeyIdentifier:@"instanceId"];

// Configure API key authorization: (authentication scheme: token)
[apiConfig setApiKey:@"YOUR_API_KEY" forApiKeyIdentifier:@"token"];
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
//[apiConfig setApiKeyPrefix:@"Bearer" forApiKeyIdentifier:@"token"];



OAIClass1InstanceApi *apiInstance = [[OAIClass1InstanceApi alloc] init];

// Updates the QR code after its expired
[apiInstance expiryWithCompletionHandler:
              ^(OAIInlineResponse2002* output, NSError* error) {
                            if (output) {
                                NSLog(@"%@", output);
                            }
                            if (error) {
                                NSLog(@"Error: %@", error);
                            }
                        }];

Documentation for API Endpoints

All URIs are relative to https://api.chat-api.com

Documentation For Authorization

instanceId

  • Type: API key
  • API key parameter name: instanceId
  • Location: URL query string

token

  • Type: API key
  • API key parameter name: token
  • Location: URL query string

Preparatory work

At the very beginning, we need to connect whatsapp with our script, so as we write the code, we check its operation. To do this, go to your personal account and get a QR code there. Next, open WhatsApp on your smartphone, go to Settings -> WhatsApp Web -> Scan a QR code.

Now we need to indicate a WebHook URL so the server can run the scrip when new messages arrive. Indicate a direct link to your script. You can’t indicate server IP only, but you can indicate the port.


The first steps for beginners

The generated code has dependencies over external libraries like UniRest. These dependencies are defined in the PodFile file that comes with the SDK. To resolve these dependencies, we use the Cocoapods package manager. Visit https://guides.cocoapods.org/using/getting-started.html to setup Cocoapods on your system. Open command prompt and type pod --version. This should display the current version of Cocoapods installed if the installation was successful.

Using command line, navigate to the directory containing the generated files (including PodFile) for the SDK. Run the command pod install. This should install all the required dependencies and create the pods directory in your project directory.

Installing dependencies using Cocoapods

Open the project workspace using the (WhatsappAPI.xcworkspace) file. Invoke the build process using Command(⌘)+B shortcut key or using the Build menu as shown below.

Building SDK using Xcode

How to Use

The generated code is a Cocoa Touch Static Library which can be used in any iOS project. The support for these generated libraries go all the way back to iOS 6.

The following section explains how to use the WhatsappAPI library in a new iOS project.

1. Starting a new project

To start a new project, left-click on the Create a new Xcode project. Create Test Project - Step 1

Next, choose Single View Application and click Next. Create Test Project - Step 2

Provide Test-Project as the product name click Next. Create Test Project - Step 3

Choose the desired location of your project folder and click Create. Create Test Project - Step 4

2. Adding the static library dependency

To add this dependency open a terminal and navigate to your project folder. Next, input pod init and press enter. Add dependency - Step 1

Next, open the newly created PodFile in your favourite text editor. Add the following line : pod 'WhatsappAPI', :path => 'Vendor/WhatsappAPI' Add dependency - Step 2

Execute pod install from terminal to install the dependecy in your project. This would add the dependency to the newly created test project. Add dependency - Step 3

How to Test

Unit tests in this SDK can be run using Xcode.

First build the SDK as shown in the steps above and naivgate to the project directory and open the WhatsappAPI.xcworkspace file.

Go to the test explorer in Xcode as shown in the picture below and click on run tests from the menu. Run tests

Other WhatsApp API SDKs

You can configure the SDK or generate other SDKs in 30+ programming languages: Chat API SDK generator.