1

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.

Pruthvi Hariharan
  • 551
  • 1
  • 6
  • 23

3 Answers3

1

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.

CharlieC
  • 502
  • 3
  • 8
  • Thanks for advice Charlie. Im definitely gonna consider it. For now I'm in trial account and trying to send a message. Since you said you are Plivo Sales engg, I have few questions for you. I'm passing this for HTTP request "https://api.plivo.com/v1/Account/\(myTrialauthid)\(myTrialAuthToken)/Message/" Im getting a response like this: Response: Could not verify your access level for that URL. You have to login with proper credentials Could you please tell me what's wrong? – Pruthvi Hariharan May 11 '16 at 17:02
  • You need to use http basic authentication when requesting the url. You should be able to write the url like this: `https://AUTH_ID:AUTH_TOKEN@api.plivo.com/v1/Account/AUTH_ID/Message/` replace `AUTH_ID` with your Plivo Auth ID and `AUTH_TOKEN` with your Plivo Auth Token. – CharlieC May 11 '16 at 17:06
  • what error message are you getting now? You can only use numbers in your Plivo account as `src` numbers. If you don't have a Plivo number, you need to buy one. Also, because you have a trial account, you need to verify every `dst` number to which you want to send an sms. – CharlieC May 11 '16 at 17:16
  • Actually its not the problem with parameters yet, its the url. It saying url not found. Any idea? – Pruthvi Hariharan May 11 '16 at 17:18
  • I've discovered that with Swift you can't use the basic url authentication like I suggested. See http://stackoverflow.com/a/24380884/4731823 for details on how to use Basic Authentication with an HTTPS request. – CharlieC May 11 '16 at 17:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/111670/discussion-between-charlieplivo-and-pruthvi-hariharan). – CharlieC May 11 '16 at 18:32
0

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

Jason Sperske
  • 29,816
  • 8
  • 73
  • 124
  • I understood that I should make use of HTTP request. But how to integrate REST API in my app? I should import Plivo for that right? But How do i achieve that? – Pruthvi Hariharan May 11 '16 at 16:14
  • Without a helper library there is nothing to import, you have to build the http request by hand. A library like Alamofire will make this easier – Jason Sperske May 11 '16 at 16:18
  • Thanks Jason. I have one more question, Will Apple allow sending a sms with only HTTP request? Will it be secure? Will my app gets rejected if I do so? – Pruthvi Hariharan May 11 '16 at 16:22
  • In effect Apple can't control what http API you call, if that call sends an SMS then Apple can't intercept it also the SMS that it sends arrives from the network carrier and to my knowledge no carrier has implemented a spam filter for received SMS. What carriers can do (to date I haven't personally encountered this but my company sends relatively few SMS messages a year, less than 1000) is deny a number if it's reported as spam by a receiver (on AT&T if you forward an unauthorized text to a specific SMS short code AT&T will try and get the sending carrier to ban the account) – Jason Sperske May 11 '16 at 16:26
  • We use Twilio for this and it works great. My understanding is if Twilio were approached by a carrier to ban a number that I used they would turn around and limit my account until I had passed a few weeks of no reported messages. To date this has not happened to us. – Jason Sperske May 11 '16 at 16:28
  • Thank you very much Jason. I was just afraid, if my app wont get accepted later. Now I feel its safe to use. Thanks again! – Pruthvi Hariharan May 11 '16 at 16:31
0

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)
AlKozin
  • 904
  • 8
  • 25