How to Delete an SMS in Objective C

The simplest way to delete 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 delete an sms in objective c
Figure 1 - How to Delete an SMS in Objective C

Objective-C code to delete sms to mobile

The Objective-C sms code sample below demonstrates how you can delete 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.

DeleteSms.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" ];
    
    Message * msg = [ [ Message alloc ] init ];
    [ msg setID : @"77edf5e7-691f-4328-a0ce-80402a44cea1" ];
    
    MessageApi * api = [ [ MessageApi alloc ] initWithConfiguration : configuration ];
    
    Boolean result = [ api DeleteMessage : msg Folder : Inbox ];
    
    NSLog(@"%hhu", 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 DeleteSms.m

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

What is in the DeleteSms.m file?

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

delete sms m directory
Figure 2 - What is inside DeleteSms.m.zip

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

To delete 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 DeleteSms.m.zip file
  5. Open the DeleteSms.xcodeproj file in Xcode
  6. Launch Ozeki SMS Gateway app on your Windows machine
  7. Run DeleteSms.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=deletemsg HTTP/1.1
Connection: keep-alive
Content-Length: 73
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: DeleteSms.m (unknown version) CFNetwork/1220.1 Darwin/20.3.0

{
	"folder": "inbox",
	"message_ids": [
		"58397f07-de21-413b-bd77-2015594c4724"
	]
}

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 11:38:29 GMT
Server: 10/10.3.123
Transfer-Encoding: chunked
 
{
	"http_code": 200,
	"response_code": "SUCCESS",
	"response_msg": "",
	"data": {
		"folder": "inbox",
		"message_ids": [
			"58397f07-de21-413b-bd77-2015594c4724"
		]
	}
}

Objective-C sms example: DeleteSms.xcodeproj

In this video, you will see how you can download the DeleteSMS 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 Objective–C project. The video is only 25 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 now!

Video 1 - How to download the DeleteSMS source code (Video tutorial)

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

how to delete sms using objective c
Figure 3 - DeleteSms.xcodeproj

How to use the code (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 70-seconds-long and it contains all the information you need to start deleting SMS messages from the Ozeki SMS Gateway. It will start with logging into the Ozeki SMS Gateway and takes you all the way to the empty inbox from where we deleted 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 code (Video tutorial)

Conclusion

This article explained the steps of SMS deleting in Objective C. Removing unwanted messages is vital in storage balancing, so doing it frequently is advised. As it could be seen, Ozeki SMS Gateway can be used really well with programming codes, demonstrating the fact that this program is easy to work with and very customizable. It also has to be mentioned that Ozeki SMS Gateway runs in an environment you control, so your contact list and data are in safe hands.

Continue reading on Ozeki's tutorial page, where more information can be found about topics like SMS scheduling and multiple SMS sending in Objective C.

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

More information