简体   繁体   English

在Swift 4中使用Twilio发送消息

[英]Sending Message with Twilio in Swift 4

I am trying to send a message with Twilio API but it is not working. 我正在尝试使用Twilio API发送消息,但是它不起作用。 I used Alamofire to send a message. 我使用Alamofire发送消息。 Getting trial account from www.twilio.com and pass related personal parameters to my program. 从www.twilio.com获取试用帐户,并将相关的个人参数传递给我的程序。 When I run the program, nothing happened. 当我运行程序时,什么也没发生。 For security reason I used fake numbers for token, SID, phone numbers etc. 出于安全原因,我将伪造的数字用于令牌,SID,电话号码等。

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 ,而应按照Twillio文档定义的方式将这些参数直接添加到您的请求中。 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. authenticate仅用于HTTP身份验证,并且用于生成响应凭证,因此,除非服务器提示输入auth,否则它将不被使用。

Also, you don't need that RunLoop.main.run() , iOS apps have a run loop automatically. 另外,您不需要RunLoop.main.run() ,iOS应用程序会自动具有运行循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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