How to call a web service in swift2 Xcode 7

Asked

Viewed 146 times

0

I’ve tried 1000 and 1 thing and still can’t find a way to call my web service and get the desired results.

Can anyone tell me the best way to connect to my web-service and call my methods in swift2 with Xcode 7?

  • your webservice returns what ?

  • he returns Json

  • Take a look at this post: http://answall.com/questions/109002/consumer-dados-json-webservice-swift-2-ios/109007#109007

  • I already tried using Alamofire however I did not install the cocoapods because it did not say to do it, and it did not work. Tomorrow early I will try and soon give a feedback. But from what I saw it is not very easy to proceed to the installation of this components. Thank you

  • On the other hand, the installation is very simple, but you can still install manually, or if you prefer to follow the @Leo Dabus solution that does not require a component

  • Know which library she uses for this JSON let json = JSON(data: data, options: .AllowFragments, error: &error)

  • To perform any, just to parse the json that uses Swifyjson

  • @Gabrielrodrigues, I took your advice and I did with Alamofire however I have a problem methods in which I have to pass parameters get Failure on request. I place on the Link my code [(http://pasteboard.co/1heVRG8P.png

  • The call is the same, already tried to check the url ?

  • Yes the call is in everything equal only with the difference that in the 1st call step parametres and in the 2nd call no, that is to say the problem lies in the passing parameters. Now I don’t know what I’m doing wrong to be making a mistake

  • goes to the github of the library: https://github.com/Alamofire/Alamofire note that there is a third parameter of the function that accepts "parameters"

  • Unfortunately I already tried and gave this error status code: 500, headers { "Cache-Control" = private; "Content-Length" = 1937; "Content-Type" = "application/xml; charset=utf-8"; Date = "Wed, 03 Feb 2016 12:54:20 GMT"; Server = "Microsoft-IIS/8.5"; "X-Aspnet-Version" = "4.0.30319"; "X-Powered-By" = "ASP.NET"; } })

  • http://www.checkupdown.com/status/E500_pt.html

  • As I understand it is a mistake of the webservice and that can only be solved there? the problem is that I have this app working in objective-c with this webservice and there is no problem. It’s only now that I’m going to swift2

  • @Hidecode puts your code that works (objective-c), the code that Voce tried and that doesn’t work (Swift)

  • Already managed, using the Alamofire, I will post my answer in the question

Show 11 more comments

1 answer

0


Using the Alamofire library I made the request as follows:

    let URL = NSURL(string: "https://****.*****.com/*****.svc/rest/GetByID")!
    let mutableURLRequest = NSMutableURLRequest(URL: URL)
    mutableURLRequest.HTTPMethod = "POST"

    let parameters = ["ID": "23"]

    do {
        mutableURLRequest.HTTPBody = try NSJSONSerialization.dataWithJSONObject(parameters, options: NSJSONWritingOptions())
    } catch {
    }

    mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")

    Alamofire.request(mutableURLRequest)
        .responseJSON{
            response in
            if let JSON = response.result.value{

                let dataSelected = [JSON.valueForKey("Price")!]         //Quando vem apenas um objecto
                print("RESULTADO \(dataSelected)")
            }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.