【问题标题】:Sending Message with Twilio in Swift 4在 Swift 4 中使用 Twilio 发送消息
【发布时间】:2019-03-12 10:41:53
【问题描述】:

我正在尝试使用 Twilio API 发送消息,但它不起作用。我使用 Alamofire 发送消息。从 www.twilio.com 获取试用帐户并将相关的个人参数传递给我的程序。当我运行程序时,什么也没发生。出于安全原因,我使用假号码作为令牌、SID、电话号码等。

这是我的代码:

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()                
        }        
    }        
}

【问题讨论】:

    标签: swift twilio alamofire message


    【解决方案1】:

    您很可能不应该使用authenticate,而是按照 Twillio 文档定义的方式直接将这些参数添加到您的请求中。 authenticate 仅用于 HTTP 身份验证,用于生成响应凭据,因此除非服务器提示进行身份验证,否则不会使用它。

    另外,你不需要RunLoop.main.run(),iOS 应用程序会自动运行循环。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多