Sending SMS via Twilio on ios?

£可爱£侵袭症+ 提交于 2019-12-03 08:43:50
Kevin Burke

According to this answer, error 1012 means that a request for authentication was canceled by the user.

It's just a hunch, but you may want to try using HTTP Basic Auth by adding an Authorization header like this: Objective-c HTTP Basic authentication instead of including the credentials in the URL string, which counts on the Objective C library to turn those into a header correctly.

Make sure that the HttpPost param is URL-encoded, so you should change

NSString *kToNumber = @"+14126620408";

to

NSString *kToNumber = @"%2B14126620408";

I've written this blog post to help you get this done quickly using Xcode 8 and Swift 3.

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

Using a server-side language of your choosing and Alamofire for HTTP requests, the request boils down to this:

@IBAction func sendData(sender: AnyObject) { 
    let headers = [
        "Content-Type": "application/x-www-form-urlencoded"
    ]

    let parameters: Parameters = [
        "To": phoneNumberField.text ?? "",
        "Body": messageField.text ?? ""
    ]

    Alamofire.request("YOUR_NGROK_URL/sms", method: .post, parameters: parameters, headers: headers).response { response in
            print(response)

    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!