Created
June 5, 2014 17:34
-
-
Save zcag/23d287805268c472e940 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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"
}