简体   繁体   中英

Sending Message with Twilio in Swift 4

I am trying to send a message with Twilio API but it is not working. I used Alamofire to send a message. Getting trial account from www.twilio.com and pass related personal parameters to my program. When I run the program, nothing happened. For security reason I used fake numbers for token, SID, phone numbers etc.

Here is my code:

import UIKit
import Alamofire    

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()                
        if let accountSID = ProcessInfo.processInfo.environment["???????"], let authToken = ProcessInfo.processInfo.environment["?????"] {                
            let url = "https://api.twilio.com/2010-04-01/Accounts/\(accountSID)/Messages"
            let parameters = ["From": "+??????????", "To": "90????????", "Body": "Hello world"]

            Alamofire.request(url, method: .post, parameters: parameters)
                .authenticate(user: accountSID, password: authToken)
                .responseJSON { response in
                    debugPrint(response)                        
            }                                
            RunLoop.main.run()                
        }        
    }        
}

Most likely you shouldn't be using authenticate but instead add those parameters to your request directly in the way defined by the Twillio docs. authenticate is only for HTTP authentication and is used to produce a response credential, so unless the server prompts for auth it will not be used.

Also, you don't need that RunLoop.main.run() , iOS apps have a run loop automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM