How to send sms from Objective C

The most simple way to send SMS from Objective C is to use the built in HTTP/Rest SMS api of Ozeki SMS Gateway. When you use this API, you will send SMS messages by issuing a HTTP Post request to the SMS gateway. The HTTP Post request will contain a message formatted in json format. The SMS gateway will send this SMS to the recipient phone, and it will return a HTTP 200 OK response to your request.

how to send sms from objective c
Figure 1 - How to send SMS from Objective C

Objective C code to send sms to mobile

The Objective C sms code sample below demonstrates how you can send SMS using the http rest sms api of Ozeki SMS Gateawy using the Objective C Ozeki.Libs.Rest library. This library is provided to you free of charge, and you may use it and modify it in any of your projects.

SendSms.m
  
#import <Foundation/Foundation.h>
#import "Ozeki.Libs.Rest.h"

int main(int argc, const char * argv[]) {
    Configuration * configuration = [ [ Configuration alloc ] init];
    [ configuration setUsername : @"http_user" ];
    [ configuration setPassword : @"qwe123" ];
    [ configuration setApiUrl : @"http://127.0.0.1:9509/api" ];
    // You have to replace the IP address with the IP of the computer which has the SMS Gateway on it!
    
    Message * msg = [ [ Message alloc ] init ];
    [ msg setToAddress : @"+36201111111" ];
    [ msg setText : @"Hello world!" ];
    
    MessageApi * api = [ [ MessageApi alloc ] initWithConfiguration : configuration ];
    
    MessageSendResult * result = [ api SendMessage: msg ];
    
    NSLog(@"%@", result);
    
    return 0;
}
	

How to use the Objective C sms example:

This Objective C sms example can be used in any Objective C core application. To use it, you must add the Ozeki.Libs.Rest.h header file and the Ozeki.Libs.Rest.m implementation file to your project. After the project reference is added, you must put the using Ozeki.Libs.Rest; directive into the header section of your Objective C source code. This will allow you to use the classes provided by the Ozeki.Libs.Rest library. You can use the Message class to create the SMS. You can use the MessageApi class to send the SMS to the SMS gateway. The SMS gateway will forward your message to the mobile network either through a wireless connection or through the Internet.

Download SendSMS.m

The source code explained in this article can be downloaded and used and modified free of charge.
Download: SendSms.m.zip (59.6Kb)

What is in the SendSms.m.zip file?

The SendSms.m.zip file contains the Ozeki.Libs.Rest library, which gives you all the tools neccessary to send and receive SMS messages. You will also find the SendSms project in the zip, which contains the example code to show you how to send an SMS. This example code is listed below.

send sms m directory
Figure 2 - What is inside SendSms.m.zip

How to send SMS from Objective C (Simple guidelines)

To send SMS from Objective C:

  1. Install a HTTP API user on a Windows machine
  2. Enable Log communication events on the Advanced tab
  3. Setup Xcode
  4. Download then extract the SendSms.m.zip file
  5. Open the SendSms.xcodeproj file in Xcode
  6. Launch Ozeki SMS Gateway app on your Windows machine
  7. Run SendSms.m Objective C code in Xcode
  8. Check the logs to see if the SMS sent

Install Ozeki SMS Gateway and create an HTTP API user

To be able to send SMS from Objective C, first you need to install Ozeki SMS Gateway. The SMS gateway can be installed on the same computer, where you develop your Objective C code in Visual studio. After installation, the next step is to connect Ozeki SMS Gateway to the mobile network. You can send a test sms from the Ozeki GUI to verify, that your mobile network connection works. The final step to prepare your environment is to create a HTTP sms api user. Create a user with a username of "http_user", and with a password of "qwe123" to make the example work without modification.

After the environment is setup, you can run your Objective C code.

HTTP API url to use send sms from Objective C

To send SMS from Objective C, your Objective C will will have to issue an HTTP request to the SMS gateway. The API url is shown below. Note that the IP address (127.0.0.1) should be replaced to the IP address of your SMS gateway. If Ozeki SMS Gateway is installed on the same computer where the Objective C sms application is running, this can be 127.0.0.1. If it is installed on a different computer it should be the IP address of that computer.

http://127.0.0.1:9509/api?action=rest
	

HTTP authentication to use send sms from Objective C

To authenticate the Objective C sms client, you need to send the username and password in a base64 encoded string to the server in a HTTP request. The format used is: base64(username+":"+password). In Objective C you can use the following code to do this encoding:

NSString * username_password = [ NSString stringWithFormat : @"%@:%@", username, password ];
NSData * encode_data = [username_password dataUsingEncoding:NSUTF8StringEncoding];
NSString * username_password_encoded  = [encode_data base64EncodedStringWithOptions : 0];
return  [ NSString stringWithFormat : @"Basic %@", username_password_encoded ];
	

For example if you encode the username 'http_user' and the password 'qwe123', you will get the following base64 encoded string: aHR0cF91c2VyOnF3ZTEyMw==. To send

HTTP request header to send SMS from Objective C

To send the SMS messages, you need to include the following lines as headers in the HTTP request. Note that we include a content type and an Authorization header.

Content-Type: application/json
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
	

HTTP request to send SMS from Objective C

To submit the SMS, your Objective C application will send an HTTP request similar to the one below. Note, that this requst contains a HTTP header part and a http body part. The HTTP body is a JSON encoded data string. It contains the recipient number and the messages text.

POST /api?action=sendmsg HTTP/1.1
Connection: keep-alive
Content-Length: 332
Content-Type: application/json
Accept: application/json
Accept-Encoding: gzip, deflate
Accept-Language: en-gb
Authorization: Basic aHR0cF91c2VyOnF3ZTEyMw==
Host: 192.168.0.14:9509
User-Agent: SendSms.m (unknown version) CFNetwork/1220.1 Darwin/20.3.0
  
{
	"messages": [
		{
			"is_view_report_requested": false,
			"create_date": "2021-07-23T07:54:47",
			"message_id": "8de52a43-b6e4-4aa9-8198-54d54f252a83",
			"time_to_send": "2021-07-23T07:54:47",
			"valid_until": "2021-07-30T07:54:47",
			"is_submit_report_requested": false,
			"to_address":"+36201111111",
			"text": "Hello world!",
			"is_delivery_report_requested": false
		}
	]
}
	

HTTP response received by the Objective C sms example

Once the SMS gateway receives this request, it will generate a HTTP response. The HTTP response will contain a status code, to indicate whether the SMS submit request was successful or not. It will also return a JSON encoded structure to provide you useful details about the messages submission.

HTTP/1.1 200 OK
User-Agent: OZEKI 10.3.123 (www.myozeki.com)
Content-Type: application/json; charset=utf8
Last-Modified: Thu, 22 Jul 2021 12:17:05 GMT
Server: 10/10.3.123
Transfer-Encoding: chunked
  
{
	"http_code": 200,
	"response_code": "SUCCESS",
	"response_msg": "Messages queued for delivery.",
	"data": {
	  "total_count": 1,
	  "success_count": 1,
	  "failed_count": 0,
	  "messages": [
	    {
	      "message_id": "8de52a43-b6e4-4aa9-8198-54d54f252a83",
	      "from_station": "%",
	      "to_address": "+36201111111",
	      "to_station": "%",
	      "text": "Hello world!",
	      "create_date": "2021-07-23 07:54:47",
	      "valid_until": "2021-07-30 07:54:47",
	      "time_to_send": "2021-07-23 07:54:47",
	      "submit_report_requested": true,
	      "delivery_report_requested": false,
	      "view_report_requested": false,
	      "tags": [
	        {
	          "name": "Type",
	          "value": "SMS:TEXT"
	        }
	      ],
	      "status": "SUCCESS"
	    }
	  ]
	}
}
	

Connect your SMS gateway to the mobile network and create an HTTP API user account

We assume, you have already installed Ozeki SMS Gateway, and you have connected it to the mobile network. In order to be able to send SMS to a mobile phone from Objective C, you need to setup an HTTP API user account in Ozeki SMS Gateway.

How to setup an HTTP API user account (Video tutorial)

In this video, we will show you how you can setup a new HTTP API user account in the Ozeki SMS Gateway. It will start with the main page of the Ozeki SMS Gateway and will take you all the way to having a working user account. You will learn how to provide a password for the user account and how to enable logging. This video is only 30 minutes long but very detailed. You will have no problem understanding it. In the video, we are using the Ozeki SMS Gateway that features a very intuitive graphical interface.

How to send SMS from Objective C using the Objective C sms api (Video tutorial)

This video shows you how to download the SendSms.m.zip file from this page, and how to download it's contents in Xcode. If watch the video, you will notice, that the contents of the SendSms.m.zip are placed into the Desktop. You will also see, that we double click on the SendSms.xcodeproj project file to open the SendSms project. The Xcode will show some warnings because the file is coming from the web. Sipmly click OK to avoid these warnings.

Objective C sms example: SendSms.xcodeproj

The example code below is part of the SendSms.xcodeproj Xcode project. An xcode In this solution there are three files in this project: SendSms.m, Ozeki.Libs.Rest.m, Ozeki.Libs.Rest.h files.

Figure 3 - SendSms.xcodeproj

How to check that the SMS has been accepted by the HTTP user

After the SMS has been submitted, it is a good idea to check your SMS gateway, to see what it has received. You can check the log by opening the HTTP user's details from the Ozeki SMS Gateway managment console. The following video shows you what to look for.

Sending SMS with the Objective C code above (Video tutorial)

In this video, you can learn how to run you code and check the events happened on the user. It will start with an empty events tab and will take you all the way to an events tab with records in it. You can always get more details about your message and connection in the events tab of the user. It will show you how to open the events tab, how to send the message and how to check the events tab again. This video is only a little longer than 1 minute and it is easy to understand. We are using the Ozeki SMS Gateway to record every details of a sending procedure.

How to check that the SMS has been sent to the mobile network

The final step in verifying the procedure is to take a look at the logs of the mobile network connection. You might have to turn on logging in the configuration of the connection before you send the message to see the logs. If logging is enabled you will see the phone number and the text of the message you have sent.

How to test if the request was accepted by the SMPP client (Video tutorial

In this video, we will check the record for the sending event in the SMPP client’s Event’s tab. You will learn how to access it and what to look for in the log. The video is only 18 seconds long so you will have no problem following it. We are using the Ozeki SMS Gateway here as well.

SMS message received on the mobile phone (Video tutorial)

In the last video, you will be able to see how receiving a message sent from the Ozeki SMS Gateway looks like. You will see an IOS phone receiving a notification about the SMS message you just sent. The video is only 8 seconds long and well-detailed.

Summary

This guide gave an insight into SMS sending in Objective C with the help of Ozeki SMS Gateway. If the article was studied carefully, messaging in Objective C is very simple. It has to be underlined that the Ozeki SMS Gateway plays an enormous part in this process because it does the transmission between the coder and the SMS receiver. Ozeki SMS Gateway is very reliable and it can be managed very easily.

Continue your studies on Ozeki's tutorial page where more can be read about topics like multiple SMS sending and SMS scheduling in Objective C.

The only thing to do now is to download Ozeki SMS Gateway and let the work begin!

More information