How to receive SMS from Objective C

The most simple way to receive 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 receive sms from objective c
Figure 1 - How to receive SMS from Objective C

Objective-C code to receive sms to mobile

The Objective-C sms code sample below demonstrates how you can receive SMS using the http rest sms api of Ozeki SMS Gateawy 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.

ReceiveSms.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 ];
    
    MessageReceiveResult * result = [ api DownloadIncoming ];
    
    NSLog(@"%@", result);
    
    for (Message * message in result.messages)
    {
        NSLog(@"%@", message);
    }
    
    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 ReceiveSms.m

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

What is in the ReceiveSms.m file?

The ReceiveSms.m 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 ReceiveSms project in the zip, which contains the example code to show you how to send an SMS. This example code is listed below.

receive sms m directory
Figure 2 - What is inside ReceiveSms.m.zip

How to receive sms from Objective-C (Quick steps)

To receive 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 ReceiveSms.m.zip file
  5. Open the ReceiveSms.xcodeproj file in Xcode
  6. Launch Ozeki SMS Gateway app on your Windows machine
  7. Run ReceiveSms.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 messages, 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.

GET /api?action=receivemsg&folder=inbox HTTP/1.1
Connection: keep-alive
Accept: application/json
Accept-Encoding: gzip, deflate
Accept-Language: en-gb
Authorization: Basic aHR0cF91c2VyOnF3ZTEyMw==
Host: 192.168.0.14:9509
If-Modified-Since: Fri, 23 Jul 2021 11:01:18 GMT
User-Agent: ReceiveSms.m (unknown version) CFNetwork/1220.1 Darwin/20.3.0

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: Fri, 23 Jul 2021 11:38:29 GMT
Server: 10/10.3.123
Transfer-Encoding: chunked
 
{
	"http_code": 200,
	"response_code": "SUCCESS",
	"response_msg": "",
	"data": {
	  "folder": "inbox",
	  "limit": "1000",
	  "data": [
	    {
	      "message_id": "85583c80-68f8-a743-bdd4-a477dcce24c1",
	      "from_connection": "http_user@localhost",
	      "from_address": "+36201111111",
	      "from_station": "%",
	      "to_connection": "http_user@localhost",
	      "to_address": "http_user",
	      "to_station": "%",
	      "text": "Hello world 1",
	      "create_date": "2021-07-23 12:04:54",
	      "valid_until": "2021-07-30 12:04:54",
	      "time_to_send": "2021-07-23 12:04:54",
	      "submit_report_requested": true,
	      "delivery_report_requested": true,
	      "view_report_requested": true,
	      "tags": [
	        {
	          "name": "Type",
	          "value": "SMS:TEXT"
	        }
	      ]
	    },
	    {
	      "message_id": "17f48ae0-ca34-2393-876a-e36446f51df1",
	      "from_connection": "http_user@localhost",
	      "from_address": "+36202222222",
	      "from_station": "%",
	      "to_connection": "http_user@localhost",
	      "to_address": "http_user",
	      "to_station": "%",
	      "text": "Hello world 2",
	      "create_date": "2021-07-23 12:04:54",
	      "valid_until": "2021-07-30 12:04:54",
	      "time_to_send": "2021-07-23 12:04:54",
	      "submit_report_requested": true,
	      "delivery_report_requested": true,
	      "view_report_requested": true,
	      "tags": [
	        {
	          "name": "Type",
	          "value": "SMS:TEXT"
	        }
	      ]
	    },
	    {
	      "message_id": "e8fa747e-8b21-08e3-9cf4-f30c2ab9074d",
	      "from_connection": "http_user@localhost",
	      "from_address": "+36203333333",
	      "from_station": "%",
	      "to_connection": "http_user@localhost",
	      "to_address": "http_user",
	      "to_station": "%",
	      "text": "Hello world 3",
	      "create_date": "2021-07-23 12:04:54",
	      "valid_until": "2021-07-30 12:04:54",
	      "time_to_send": "2021-07-23 12:04:54",
	      "submit_report_requested": true,
	      "delivery_report_requested": true,
	      "view_report_requested": true,
	      "tags": [
	        {
	          "name": "Type",
	          "value": "SMS:TEXT"
	        }
	      ]
	    }
	  ]
	}
}

How to download the project (Video tutorial)

In this video, we show you how you can download the project files required to receive SMS messages in Objective-C. It will start with this tutorial page and ends with the opened source code. You will learn where to click to download the files and what to use when opening the code to edit. The video is only 22 seconds long but it contains all the information you need to receive SMS messages in Objective-C.

Video 1 - How to download and open the solution above

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

how to receive sms using objective c
Figure 3 - ReceiveSms.xcodeproj

How to use the project (Video tutorial)

In the next video, we present to you how to use the code and what happens when you use it. The video is 55 seconds long but it contains all the information you need to start working with the project. It will start with logging into the Ozeki SMS Gateway and takes you all the way to the received messages. You will learn how to open and run the code and check the log after it. The Ozeki SMS Gateway offers great user experience due to the intuitive and easy to learn graphical interface.

Video 2 - How to use the ReceiveSms.xcodeproj project

Conclusion

This guide was meant to explain the steps of SMS receiving in Objective C with the help of Ozeki SMS gateway. After reading this article, transferring messages from the inbox to an Objective C program should not cause any kind of problem. By doing this, you can make sure that the SMS messages are in their destined place. Ozeki SMS Gateway can be downloaded from our website and can be used in a trial period free of charge.

Continue reading on Ozeki's tutorial page, where more information can be found about topics like SMS deleting 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