Skip to content

Instantly share code, notes, and snippets.

@cagdassalur
Created June 5, 2014 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cagdassalur/23d287805268c472e940 to your computer and use it in GitHub Desktop.
Save cagdassalur/23d287805268c472e940 to your computer and use it in GitHub Desktop.
import UIKit
import Foundation
class ViewController: UIViewController, NSURLConnectionDelegate {
@IBOutlet var tvPw : UITextField
@IBOutlet var tvNo : UITextField
@IBOutlet var btGiriş : UIButton
@lazy var data = NSMutableData()
override func viewDidLoad(){
super.viewDidLoad()
btGiriş.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
}
func startConnection(){
//let urlPath: String = "http://binaenaleyh.net/dusor/?usr=\(tvNo.text)&pw=\(tvPw.text)&json=true"
let urlPath: String = "http://binaenaleyh.net/dusor/" // back-end bitene kadar geçici
var url: NSURL = NSURL(string: urlPath)
var request: NSURLRequest = NSURLRequest(URL: url)
var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)
connection.start()
// TODO: butonu disable et
}
func connection(connection: NSURLConnection!, didReceiveData data: NSData!){
self.data.appendData(data)
}
func buttonAction(sender: UIButton!){
println(tvNo.text + ":" + tvPw.text)
startConnection()
}
func connectionDidFinishLoading(connection: NSURLConnection!) {
println("Connection finished loading.")
var err: NSError
//println(data)
var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
println(jsonResult)
/*if jsonResult.count>0 && jsonResult["results"].count>0 {
var results: NSArray = jsonResult["results"] as NSArray
self.tableData = results
self.appsTableView.reloadData()
}*/
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
@marco-purplefox
Copy link

HI, thanks for the example. Strangest thing is, I get errors (I am a complete noob on swift, I am a C# progger)... What I tried to do, is to build a Proxy... I have put your code into a class, .... The thing is, when I set startImmediately=true I get "EXC_BAD_ACCESS, code=2". When I set it to false, the connectionDidFinishloading is never being executed... I think I understand why this happens, but don't see how I should fix this...

like so:

import Foundation

var srvConn : ServiceConnector = ServiceConnector()
class ServiceConnector: NSObject, NSURLConnectionDataDelegate {
var data = NSMutableData()
var jsonpurl: String = "http://purplefox-rs-webapi.azurewebsites.net/api/ViewNavigation?callback=fetch"
var jsonurl: String = "http://purplefox-sp-webapi.azurewebsites.net/api/incident"

func startConnection(){
    var baseUrl:NSURL = NSURL(string:jsonurl)
    var request: NSURLRequest = NSURLRequest(URL: baseUrl)
    var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: true)

}

func connection(didReceiveResponse: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {

    // Recieved a new request, clear out the data object
    self.data = NSMutableData()

}

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



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(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary



    println(jsonResult)
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment