I recently made a single-screen web-based application using PHP and AJAX. I want to port this app to iOS, and have started to teach myself Swift. I had the application working perfectly in Swift 1, but since I updated to xCode 7.2 and Swift 2 so that I could try the app on my iPhone with iOS 9, the code was completely non-functional. From here, I have new code now, and as far as I can see, it should work, but I am getting an empty response. What is wrong?
我最近使用PHP和AJAX制作了一个基于Web的单屏幕应用程序。我想把这个应用程序移植到iOS,并开始自学Swift。我让应用程序在Swift 1中运行得很好,但是自从我更新到xCode 7.2和Swift 2以便我可以在我的iPhone上使用iOS 9试用该应用程序时,代码完全没有功能。从这里开始,我现在有了新的代码,据我所知,它应该可以工作,但我得到了一个空的响应。怎么了?
import UIKit
import Foundation
class LoginViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var username_textField: UITextField!
@IBOutlet weak var password_TextFieldOutlet: UITextField!
@IBAction func password_TextFieldAction(sender: AnyObject) {
let username = username_textField.text
let password = password_TextFieldOutlet.text
var urlRequest = "~~URL~~/login.php?username=\(username)&password=\(password)"
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The idea is I was to get my data from the database and see if "success" (a boolean value in the db) returns 1 or 0. If it returns 1, then perform a segue to next page, if not, create an alert saying incorrect username and password.
我的想法是从数据库中获取数据并查看“success”(db中的布尔值)是返回1还是0.如果返回1,则执行segue到下一页,如果没有,则创建一个警报说不正确的用户名和密码。
Thank you in advance!
先感谢您!
2 个解决方案
#1
0
-
Create the url
创建网址
let url = NSURL(string: "http://google.com")
let url = NSURL(字符串:“http://google.com”)
-
Create the request
创建请求
let request = NSMutableUrlRequest(url: url!)
let request = NSMutableUrlRequest(url:url!)
-
Add data and request methods
添加数据和请求方法
request.HTTPMethod = "POST"
request.HTTPBody = "username=\(username text.text!)".dataUsingEncoding(NSUTF8StringEncoding)
request.HTTPMethod =“POST”request.HTTPBody =“username = \(username text.text!)”。dataUsingEncoding(NSUTF8StringEncoding)
-
Send the connection
发送连接
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response, data, error) -> Void in if data != nil && error == nil { if let response = String(data: data!, encoding: NSUTF8StringEncoding) { //here is where you now have your response } })
#2
0
Use NSURLConnectionDelegate
or AFNetworking
for networking requests. Following is an implementation using NSURLConnection
.
使用NSURLConnectionDelegate或AFNetworking进行网络请求。以下是使用NSURLConnection的实现。
I have implemented LoginViewController
class for you. Hook the following loginButtonAction
to 'Login/SignIn/Done' button on your UI.
我已经为你实现了LoginViewController类。将以下loginButtonAction挂钩到UI上的“Login / SignIn / Done”按钮。
import UIKit
import Foundation
class LoginViewController: UIViewController, NSURLConnectionDelegate {
@IBOutlet weak var username_textField: UITextField!
@IBOutlet weak var password_TextFieldOutlet: UITextField!
@lazy var data = NSMutableData()
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
startConnection()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func startConnection(){
let username = username_textField.text
let password = password_TextFieldOutlet.text
let urlPath: String = "~~URL~~/login.php?username=\(username)&password=\(password)"
var url: NSURL = NSURL(string: urlPath)
var request: NSURLRequest = NSURLRequest(URL: url)
var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)
connection.start()
}
func connection(connection: NSURLConnection!, didReceiveData data: NSData!){
self.data.appendData(data)
}
func loginButtonAction(sender: UIButton!){
startConnection()
}
func connectionDidFinishLoading(connection: NSURLConnection!) {
var err: NSError
var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
//Here I am printing the results, you can check the results and based on response Bool you can perform segue to next view controller.
println(jsonResult)
}
}
#1
0
-
Create the url
创建网址
let url = NSURL(string: "http://google.com")
let url = NSURL(字符串:“http://google.com”)
-
Create the request
创建请求
let request = NSMutableUrlRequest(url: url!)
let request = NSMutableUrlRequest(url:url!)
-
Add data and request methods
添加数据和请求方法
request.HTTPMethod = "POST"
request.HTTPBody = "username=\(username text.text!)".dataUsingEncoding(NSUTF8StringEncoding)
request.HTTPMethod =“POST”request.HTTPBody =“username = \(username text.text!)”。dataUsingEncoding(NSUTF8StringEncoding)
-
Send the connection
发送连接
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response, data, error) -> Void in if data != nil && error == nil { if let response = String(data: data!, encoding: NSUTF8StringEncoding) { //here is where you now have your response } })
#2
0
Use NSURLConnectionDelegate
or AFNetworking
for networking requests. Following is an implementation using NSURLConnection
.
使用NSURLConnectionDelegate或AFNetworking进行网络请求。以下是使用NSURLConnection的实现。
I have implemented LoginViewController
class for you. Hook the following loginButtonAction
to 'Login/SignIn/Done' button on your UI.
我已经为你实现了LoginViewController类。将以下loginButtonAction挂钩到UI上的“Login / SignIn / Done”按钮。
import UIKit
import Foundation
class LoginViewController: UIViewController, NSURLConnectionDelegate {
@IBOutlet weak var username_textField: UITextField!
@IBOutlet weak var password_TextFieldOutlet: UITextField!
@lazy var data = NSMutableData()
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
startConnection()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func startConnection(){
let username = username_textField.text
let password = password_TextFieldOutlet.text
let urlPath: String = "~~URL~~/login.php?username=\(username)&password=\(password)"
var url: NSURL = NSURL(string: urlPath)
var request: NSURLRequest = NSURLRequest(URL: url)
var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)
connection.start()
}
func connection(connection: NSURLConnection!, didReceiveData data: NSData!){
self.data.appendData(data)
}
func loginButtonAction(sender: UIButton!){
startConnection()
}
func connectionDidFinishLoading(connection: NSURLConnection!) {
var err: NSError
var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
//Here I am printing the results, you can check the results and based on response Bool you can perform segue to next view controller.
println(jsonResult)
}
}