TwilioDevEd / twiliochat-swift

Swift implementation of Twilio Chat
https://www.twilio.com/docs/tutorials/walkthrough/ip-chat/ios/swift
MIT License
46 stars 18 forks source link

TokenRequestHandler fetchToken doesnt work #12

Open otymartin opened 7 years ago

otymartin commented 7 years ago

@joliveros @brentschooley @mcelicalderon No matter what, I can't get my URL to hit my server endpoint. My server is working properly (nodejs), I tested this by sending a regular get request to my http://localhost/token which works. The problems occurs when I attempt to send a POST request with parameters. Perhaps a swift 3 example to show how its done? The method found in the code example doesnt work either.


let tokenRequestUrl = "http://0.0.0.0:1337/token"
 let params = ["device": device, "identity": "1234"]
Alamofire.request(Twilio.tokenRequestUrl, method: .post, parameters: params, encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in
            print(response)
        }

FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))
dprothero commented 7 years ago

Your phone (and likely simulator) isn't going to be able to access your localhost. You need to create a publicly accessible URL to use. The easiest way to do this for a local development machine is to use ngrok. Once you've installed ngrok, run it like so:

$ ngrok http 1337

This will give you a public URL like https://abcdef123.ngrok.io. Modify the code to use that URL:

let tokenRequestUrl = "https://abcdef123.ngrok.io/token"

That should get you going. Sorry for not seeing this earlier.