I have a TCP server that listens to incoming commands and communicates with the database. How can I send to him requests from ios application? I work with swift 2 .

In other words, you need to implement the following: The user enters the username and password, trying to log into your account. At this time, a request is sent to the server - "Check if there is such a user in the database". He does this, and sends the answer back to the ios app.

I just can not figure out how to do this. As far as I understand, I need to work with streams, but I did not find anything worthwhile about it on ios .

  • stackoverflow.com/questions/19003063/… mb is there anything useful in the answers? - Schullz
  • Yes, I watched this topic. Alas, there is an example in Objective-C that does not suit me - Andrey Morozov

2 answers 2

If I understand you correctly, then you do not need threads at all. Here it is necessary to use blocks. Let's say you send a request and turn on the download indicator, and when a response comes you stop downloading and, depending on the result, use the success or failure block from the server. I advise you to use AFNetworking .

Something like

 let manager = AFHTTPRequestOperationManager() manager.GET( "http://myServerUrl.com", parameters: ["email":"myemail@gmail.com", "password":"1234Password"], success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in //TODO - make login action }, failure: { (operation: AFHTTPRequestOperation!, error: NSError!) in // TODO - show error message } ) 
  • I have a TCP server, that is, it waits for TCPClient to connect to it, and they begin to communicate with each other through the stream. So I have implemented a client-server application in C# . And here I want the ios application to also connect to the server, and also communicate with it through the stream. Is it possible I do not understand how to send GET/POST requests to my server, because I do not have its URL - I have its IP and Port number. - Andrey Morozov
  • @AndreyMorozov turns out that you have communication through sockets? - Vitali Eller
  • It turns out that yes. I can attach the main piece of server code, if necessary. - Andrey Morozov
  • one
    Then you need to work with sockets. See here: github.com/socketio/socket.io-client-swift or github.com/swiftsocket/SwiftSocket . And also a good article: appcoda.com/socket-io-chat-app - Vitali Eller
  • Yes, I saw these libraries. Already I am poking around with them for the third day, and in no way is it possible to achieve the desired. Did you work with them yourself? - Andrey Morozov

As far as I understand, you need to make an HTTP request to authorize the user to your server and get some kind of response. Alamofire will work for you with Swift networking .

The simplest example of performing a GET request:

 Alamofire.request(.GET, "https://yourserver.org/get", parameters: ["name": "user", "pass": "12345"]) .responseJSON { response in print(response.result) // result of response serialization } 

More detailed examples of queries are given in the description .

  • Look at my comment above, I ask you the same thing. I would be glad if you can clarify the situation. - Andrey Morozov