How to schedule an SMS in Objective C

The simplest way to send scheduled 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's phone, and it will return a HTTP 200 OK response to your request.

how to schedule an sms in objective c
Figure 1 - How to schedule an SMS in Objective C

Objective-C code to send scheduled 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 Gateway using the 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.

SendScheduledSms.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://192.168.0.14:9509/api" ];
    
    MessageApi * api = [ [ MessageApi alloc ] initWithConfiguration : configuration ];
    
    Message * msg = [ [ Message alloc ] init ];
    [ msg setToAddress : @"+36201111111" ];
    [ msg setText : @"Hello world 1" ];
    
    NSDateFormatter * dateFormat = [ [ NSDateFormatter alloc ] init ];
    [ dateFormat setDateFormat : @"yyyy-MM-dd HH:mm:ss" ];
    
    [ msg setTimeToSend: [ dateFormat dateFromString : @"2021-07-23 10:00:00" ] ] ;
    
    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 SendScheduledSms.m

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

What is in the SendScheduledSms.m file?

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

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

How to send scheduled sms from Objective-C (Quick steps)

To send scheduled 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 SendScheduledSms.m.zip file
  5. Open the SendScheduledSms.xcodeproj file in Xcode
  6. Launch Ozeki SMS Gateway app on your Windows machine
  7. Run SendScheduledSms.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 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 messages, your Objective-C application will send an HTTP request similar to the one below. Note, that this request contains a HTTP header part and a http body part. The HTTP body is a JSON encoded data string. It contains the recipient's number and the message's text.

POST /api?action=sendmsg HTTP/1.1
Connection: keep-alive
Content-Length: 333
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: SendScheduledSms.m (unknown version) CFNetwork/1220.1 Darwin/20.3.0
 
{
	"messages": [
		{
			"is_view_report_requested": false,
			"create_date": "2021-07-23T10:16:59",
			"message_id": "cab9eb77-309b-4aa4-99ba-cdac32388f7d",
			"time_to_send": "2021-07-23T10:20:00",
			"valid_until": "2021-07-30T10:16:59",
			"is_submit_report_requested": false,
			"to_address": "+36201111111",
			"text": "Hello world 1",
			"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 message's submission.

HTTP/1.1 200 OK
User-Agent: OZEKI 10.3.123 (www.myozeki.com)
Content-Type: application/json; charset=utf8
Last-Modified: Fri, 23 Jul 2021 09:58:42 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": "cab9eb77-309b-4aa4-99ba-cdac32388f7d",
	      "from_station": "%",
	      "to_address": "+36201111111",
	      "to_station": "%",
	      "text": "Hello world 1",
	      "create_date": "2021-07-23 10:16:59",
	      "valid_until": "2021-07-30 10:16:59",
	      "time_to_send": "2021-07-23 10:20:00",
	      "submit_report_requested": true,
	      "delivery_report_requested": false,
	      "view_report_requested": false,
	      "tags": [
	        {
	          "name": "Type",
	          "value": "SMS:TEXT"
	        }
	      ],
	      "status": "SUCCESS"
	    }
	  ]
	}
}

Objective-C sms example: SendScheduledSms.xcodeproj

How to download the example code (Video tutorial)

In this video, you will see how you can download the scheduled SMS sending source code. It will start with the download page and will take you all the way to opening the project. You will learn how to download it and what program to use to open the project. The video is only 28 seconds long but very detailed so you will have no problem with following the steps. Don’t waste more time. Let’s start sending SMS!

Video 1 - How to download and open the solution above (Video tutorial)

The example code below is part of the SendScheduledSms.xcodeproj project. In the zip there is only one project: SendScheduledSms.xcodeproj, and three files: SendScheduledSms.m, Ozeki.Libs.Rest.h, Ozeki.Libs.Rest.m.

how to send scheduled sms using objective c
Figure 3 - SendScheduledSms.xcodeproj

How to use the code (Video tutorial)

In this video we will present you how to use the SendScheduledSMS project. It will start with the empty events tab in the Ozeki SMS Gateway and will take you all the way to the event tab with the log of the first sent SMS message. You will learn how to open the events tab and how to run the scheduled SMS sending code in Xcode. This video is great because it takes 1 minute to watch but it contains all the information you need.

Video 2 - How to use the SendScheduledSms.xcodeproj project (Video tutorial)

Summary

This article explained how to schedule messages in Objective C. This is a really useful function if you want to send out SMS in specific time. This solution helps you or your company to be more customer friendly because the messages will be delivered when the clients are not busy. Important to note that Ozeki SMS Gateway works in any country and can send and receive SMS through various mobile connections.

Make sure that the reading is not finished here, visit Ozeki's tutorial page where more information can be found about topics like SMS receiving and deleting in Objective C.

Now the only thing to do is to download Ozeki SMS Gateway and start working!

More information