Organizational Research By

Surprising Reserch Topic

nsurlconnection using ios swift


nsurlconnection using ios swift  using -'ios,swift,nsurlconnection,nsurlrequest'

I am trying to follow this tutorial and connect to a JSON api using Swift and NSURLConnection. I can see that it is hitting the url but the connectionDidFinishLoading does not seem to fire.

import UIKit

class Remote: NSObject {

    var host = "http://localhost:3000"
    var query = String()
    var data: NSMutableData = NSMutableData()

    func connect(query:NSString) {
        self.query = query
        var url = self.document()
        var conn = NSURLConnection(request: url, delegate: self, startImmediately: true)
    }

    func endpoint() -> NSURL {
        var query = self.host + self.query
        return NSURL(string: query)
    }

    func document() -> NSURLRequest {
        return NSURLRequest( URL: self.endpoint() )
    }

    func connection(didReceiveResponse: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
        // Recieved a new request, clear out the data object
        self.data = NSMutableData()
    }

    func connection(connection: NSURLConnection!, didReceiveData conData: NSData!) {
        // Append the recieved chunk of data to our data object
        self.data.appendData(conData)
    }

    func connectionDidFinishLoading(connection: NSURLConnection!) {
        // Request complete, self.data should now hold the resulting info
        // Convert the retrieved data in to an object through JSON deserialization
        var err: NSError
        var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(self.data, options:    NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary

        println(jsonResult.count)
    }


}


// Excecute the code

var remote = Remote()
remote.connect("/apis")


At this point I am just trying to see the data that is returned. I would like to hook it into a view controller once I am sure this is working. Is there something wrong with doing it this way and that is causing an issue?
    
asked Oct 11, 2015 by gauravsinghal83
0 votes
8 views



Related Hot Questions



Government Jobs Opening


...