How to send multiple sms from Objective C
The simplest way to send multiple 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.
Objective-C code to send multiple 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.
SendMultipleSms.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" ]; // You have to replace the IP address with the IP of the computer which has the SMS Gateway on it! MessageApi * api = [ [ MessageApi alloc ] initWithConfiguration : configuration ]; Message * msg1 = [ [ Message alloc ] init ]; [ msg1 setToAddress : @"+36201111111" ]; [ msg1 setText : @"Hello world 1" ]; Message * msg2 = [ [ Message alloc ] init ]; [ msg2 setToAddress : @"+36202222222" ]; [ msg2 setText : @"Hello world 2" ]; Message * msg3 = [ [ Message alloc ] init ]; [ msg3 setToAddress : @"+36203333333" ]; [ msg3 setText : @"Hello world 3" ]; NSMutableArray<Message *> * messages = [ [ NSMutableArray<Message *> alloc ] initWithObjects: msg1, msg2, msg3, nil ]; MessageSendResults * result = [ api SendMessages: messages ]; 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 SendMultipleSms.m
The source code explained in this article can be downloaded and used and modified free of charge.
Download: SendMultipleSms.m.zip (48.4Kb)
What is in the SendMultipleSms.m file?
The SendMultipleSms.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 SendMultipleSms project in the zip, which contains the example code to show you how to send an SMS. This example code is listed below.
How to send multiple sms from Objective-C (Quick steps)
To send multiple sms from Objective-C:
- Install a HTTP API user on a Windows machine
- Enable Log communication events on the Advanced tab
- Setup Xcode
- Download then extract the SendMultipleSms.m.zip file
- Open the SendMultipleSms.xcodeproj file in Xcode
- Launch Ozeki SMS Gateway app on your Windows machine
- Run SendMultipleSms.m Objective C code in Xcode
- 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 messages text.
POST /api?action=sendmsg HTTP/1.1 Connection: keep-alive Content-Length: 971 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: SendMutipleSms.m (unknown version) CFNetwork/1220.1 Darwin/20.3.0 { "messages": [ { "is_view_report_requested": false, "create_date": "2021-07-23T09:23:25", "message_id": "9c6a7eb2-37b5-4fea-a75f-c0cdaba85466", "time_to_send": "2021-07-23T09:23:25", "valid_until": "2021-07-30T09:23:25", "is_submit_report_requested": false, "to_address": "+36201111111", "text": "Hello world 1", "is_delivery_report_requested": false }, { "is_view_report_requested": false, "create_date": "2021-07-23T09:23:25", "message_id": "8f2ff6cb-417a-4650-a474-8adc4b005cc4", "time_to_send": "2021-07-23T09:23:25", "valid_until": "2021-07-30T09:23:25", "is_submit_report_requested": false, "to_address": "+36202222222", "text": "Hello world 2", "is_delivery_report_requested": false }, { "is_view_report_requested": false, create_date": "2021-07-23T09:23:25", "message_id": "39b54837-a7e5-4319-a154-25d25bf3d920", "time_to_send": "2021-07-23T09:23:25", "valid_until": "2021-07-30T09:23:25", "is_submit_report_requested": false, "to_address": "+36203333333", "text": "Hello world 3", "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:21:44 GMT Server: 10/10.3.123 Transfer-Encoding: chunked { "http_code": 200, "response_code": "SUCCESS", "response_msg": "Messages queued for delivery.", "data": { "total_count": 3, "success_count": 3, "failed_count": 0, "messages": [ { "message_id": "9c6a7eb2-37b5-4fea-a75f-c0cdaba85466", "from_station": "%", "to_address": "+36201111111", "to_station": "%", "text": "Hello world 1", "create_date": "2021-07-23 09:23:25", "valid_until": "2021-07-30 09:23:25", "time_to_send": "2021-07-23 09:23:25", "submit_report_requested": true, "delivery_report_requested": false, "view_report_requested": false, "tags": [ { "name": "Type", "value": "SMS:TEXT" } ], "status": "SUCCESS" }, { "message_id": "8f2ff6cb-417a-4650-a474-8adc4b005cc4", "from_station": "%", "to_address": "+36202222222", "to_station": "%", "text": "Hello world 2", "create_date": "2021-07-23 09:23:25", "valid_until": "2021-07-30 09:23:25", "time_to_send": "2021-07-23 09:23:25", "submit_report_requested": true, "delivery_report_requested": false, "view_report_requested": false, "tags": [ { "name": "Type", "value": "SMS:TEXT" } ], "status": "SUCCESS" }, { "message_id": "39b54837-a7e5-4319-a154-25d25bf3d920", "from_station": "%", "to_address": "+36203333333", "to_station": "%", "text": "Hello world 3", "create_date": "2021-07-23 09:23:25", "valid_until": "2021-07-30 09:23:25", "time_to_send": "2021-07-23 09:23:25", "submit_report_requested": true, "delivery_report_requested": false, "view_report_requested": false, "tags": [ { "name": "Type", "value": "SMS:TEXT" } ], "status": "SUCCESS" } ] } }
Objective-C sms example: SendMultipleSms.xcodeproj
How to download and open the solution above (Video tutorial)
In this video, we will show you how you can download the example project and open it. It will start with your browser on the tutorial page and will end with the example project on your pc. You will learn where to click to download the file and where to look for after the download process. The video is only 30 seconds long so you will have no problem following the steps.
The example code below is part of the SendMultipleSms.xcodeproj project. In the zip there is only one project: SendMultipleSms.xcodeproj, and three files: SendMultipleSms.m, Ozeki.Libs.Rest.h, Ozeki.Libs.Rest.m.
How to use the SendMultipleSms.xcodeproj project
In the following video, we present you how to use the SendMultipleSMS project file. It will start with the empty log file of the HTTP user in Ozeki SMS Gateway and will end with the record of SMS sending. You will learn how to launch the SMS sending application and then check the log of the user. The video is nearly 1 minute long but very detailed so you will have no problem understanding it. In the video, we are using the Ozeki SMS Gateway that offers a very intuitive graphical interface for better user experience.
Conclusion
This guide showed the steps of multiple SMS sending in Objective C with the help of Ozeki SMS Gateway. With this in-depth article, reaching out to several customers at once is fast and simple. Using this function enables message sending to more people, making information exchange faster. Ozeki SMS Gateway works with high performance, meaning it can send up to 1000 SMS per second, so multiple SMS sending in Objective C is very easy with this program.
Make sure that you don't finish reading here, visit Ozeki's tutorial page to learn about similar topics, like SMS scheduling and receiving in Objective C.
The next thing to do is to download Ozeki SMS Gateway and start working now!
More information
- Objective-C send SMS with the HTTP rest API (code sample)
- Objective-C send multiple SMS with the HTTP rest API (code sample)
- Objective-C schedule SMS with the HTTP rest API (code sample)
- Objective C receive SMS with the HTTP rest API (code sample)
- Objective C delete SMS with the HTTP rest API (code sample)
- How to download the latest Objective C SMS library from Github