Pruthvi Hariharan
Pruthvi Hariharan

Reputation: 551

Sending sms from iOS app using Plivo API swift

How do I use plivo SMS API in my iOS app using Swift. I read lot of documentation regarding this. I'm not sure how to implement it on swift.

var plivo = require('plivo');
var p = plivo.RestAPI({
 authId: 'Your AUTH_ID',
authToken: 'Your AUTH_TOKEN'
});

var params = {
'src': '1111111111', // Sender's phone number with country code
'dst' : '2222222222', // Receiver's phone Number with country code
'text' : "Hi, text from Plivo", // Your SMS Text Message - English
//'text' : "こんにちは、元気ですか?", // Your SMS Text Message - Japanese
//'text' : "Ce est texte généré aléatoirement", // Your SMS Text Message - French
'url' : "http://example.com/report/", // The URL to which with the status of the message is sent
'method' : "GET" // The method used to call the url
};

// Prints the complete response
p.send_message(params, function (status, response) {
console.log('Status: ', status);
console.log('API Response:\n', response);
console.log('Message UUID:\n', response['message_uuid']);
console.log('Api ID:\n', response['api_id']);
});

The above code is in Node.js, I want it to write in swift, and also I'm not sure how to integrate plivo into iOS app. I'm developing an app where it should send a sms to admin whenever user requests an order. So I just want the outgoing message from Plivo to admin. Any suggestions is really helpfull.

Upvotes: 1

Views: 863

Answers (3)

AlKozin
AlKozin

Reputation: 916

If you want to use Rest API you can do like this:

lazy var plivoRestAPI: PlivoRest = {
    let rest = PlivoRest(authId: Constants.Credentials.Plivo.AuthId,
                         andAuthToken: Constants.Credentials.Plivo.AuthToken)!
    rest.delegate = self
    return rest
}()

let params = ["to": phoneNumberToCall,
              "from": "1111111111",
              "answer_url": "",
              "answer_method": "GET"]
plivoRestAPI.callOutbound(params)

Upvotes: 0

CharlieC
CharlieC

Reputation: 542

Plivo Sales Engineer here. Our recommendation is to host a server to which your iOS app would make a request to send the SMS. Then your server can use a Plivo helper library to make the API request which would send the SMS. This way your AuthID and AuthToken are stored on your server (to which only you have complete access) and you don't expose your Plivo credentials in your iOS app.

If you make a direct request of the Plivo API from your iOS app, then users potentially could find and misuse your credentials.

tl;dr - don't put your authentication credentials in places other people can read it.

Upvotes: 1

Jason Sperske
Jason Sperske

Reputation: 30416

So without a helper library you will have to just make use of their REST API. From their site, sending an SMS can be done by POSTing your information to

https://api.plivo.com/v1/Account/{auth_id}/Message/

As for the Swift part take a look at NSMutableURLRequest or if you need help with the networking request you can look at Alamofire

Upvotes: 0

Related Questions