Before you start
- You need a ClickSend account with credit to start sending. If you don’t have an account, create one here.
- If you need to add more credit to your account, you can top up here. You’ll be credited $2 AUD free credit when you sign up.
Installation
Install via Cocoapods
CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website.
To integrate ClickSendSwift
into your Xcode project using CocoaPods, specify it in your Podfile:
pod 'ClickSendSwift'
Update Swift Version
The ClickSend SDK was built using Swift Version 4.2.
To update the language version, select Pods within the project from the left panel > Targets > ClickSendSwift > Swift Compiler - Language > Choose Swift 4.2. Repeat these steps for Alamofire.
Send your first message
Below is the full snippet you can use to send an SMS. It includes placeholders that will need to be replaced when implementing the code in your project.
Placeholder | Replace with |
---|---|
| Your ClickSend Username. Find it here. |
| Your ClickSend API key. Find it here. |
| The origin identifier for your API request, which could be the name of your application or the source location for the request. |
| The content of your SMS message. |
| The phone number you're sending the message to, including the country code. |
import ClickSendSwift
import Alamofire
if let authHeader = Request.authorizationHeader(user: "USERNAME", password: "API_KEY") {
ClickSendClientAPI.customHeaders = [authHeader.key : authHeader.value]
}
let message = SmsMessage(
source: "SOURCE",
body: "MESSAGE",
to: "TO_PHONE_NUMBER")
let smsCollection = SmsMessageCollection(messages: [message])
SMSAPI.smsSendPost(smsMessages: smsCollection) { (dataString, error) in
guard let dataString = dataString else {
print(error!)
return
}
if let data = dataString.data(using: String.Encoding.utf8) {
do {
if let dictonary = try (JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary) {
print(dictonary)
} else {
print("bad json")
}
} catch let error as NSError {
print(error)
}
}
}
The code explained
Import libraries
Prepare your environment to use the ClickSend API by importing the SDK and required libraries.
import ClickSendSwift
import Alamofire
Authorisation
Next, add your API credentials so you can use the ClickSend API in your project.
In the following snippet, simply replace the USERNAME
and API_KEY
placeholders with your own Username and API key. These can be found in your Dashboard here.
if let authHeader = Request.authorizationHeader(user: "USERNAME", password: "API_KEY") {
ClickSendClientAPI.customHeaders = [authHeader.key : authHeader.value]
}
Configure your message
It’s time to configure the content of your message and specify who you’re sending to. This code includes everything you need to send an SMS message. To see what other parameters you can include, see our full API reference.
Now, it’s a simple matter of a few tweaks:
- Replace
SOURCE
with your preferred source (eg. the name of your application). This is not seen by recipients, but will help you to identify messages sent from various applications. - Replace
MESSAGE
with your own message. - Replace
TO_PHONE_NUMBER
with your own mobile number, including country code. For example, if you have an Australian number, it will start with +61.
let message = SmsMessage(
source: "SOURCE",
body: "MESSAGE",
to: "TO_PHONE_NUMBER")
Create an SMS message collection
Next, we’ll create a collection and add the SmsMessage
that you created earlier.
let smsCollection = SmsMessageCollection(messages: [message])
Send SMS message
Finally, try to call the smsSendPost
method using the smsCollection
you created earlier. If everything works as expected then it will print the formatted response.
If an exception occurs during the API call, the completion handler will log the outputted details. This structure helps you handle both successful API calls and potential errors that might occur during the process.
SMSAPI.smsSendPost(smsMessages: smsCollection) { (dataString, error) in
guard let dataString = dataString else {
print(error!)
return
}
if let data = dataString.data(using: String.Encoding.utf8) {
do {
if let dictonary = try (JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary) {
print(dictonary)
} else {
print("bad json")
}
} catch let error as NSError {
print(error)
}
}
}