【问题标题】:Swift: Twilio send SMS, A \'To\' phone number is requiredSwift:Twilio 发送短信,需要“收件人”电话号码
【发布时间】:2021-06-24 19:27:22
【问题描述】:

您好,我正在尝试使用 Twilio 通过 swift 发送短信。这是下面的功能。出于显而易见的原因,我遗漏了我的帐户信息。

    func sendMessage(Message: String, PhoneNumber: String) -> Int
    {
        let accountSID: String =
        let authToken: String =
        let url = "https://api.twilio.com/2010-04-01/Accounts/\(accountSID)/Messages"
            
        let parameters = ["From": "+12155154741", "To": "+15712696239", "Body": Message]
            
        AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default).authenticate(username: accountSID, password: authToken).responseString {response in debugPrint(response)}
            
        return 1
    }

我收到的错误是“需要‘收件人’电话号码。”我试过没有+1的数字。我很困惑,我在这里错过了一些简单的东西吗?

【问题讨论】:

    标签: swift twilio


    【解决方案1】:

    您需要对参数使用 url 编码而不是 JSON。 正如 Twilio 文档中所建议的那样 https://www.twilio.com/docs/sms/send-messages 请参阅 CURL 示例。 我不确定你是否也需要 auth 部分。

    或者看看这里 - https://www.twilio.com/blog/2016/11/how-to-send-an-sms-from-ios-in-swift.html

    【讨论】:

    • 感谢您的快速回复和正确答案我真的很感激!
    • 这里是 Twilio 开发者传道者。我能否建议您遵循链接博客文章中的建议,不要在 iOS 应用程序中嵌入或使用您的 Twilio 帐户 SID 和身份验证令牌,因为这是一个潜在的安全问题。
    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 2013-12-04
    • 2014-02-16
    • 2016-03-15
    • 2015-01-04
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多