2016-03-10 91 views
0

我最近使用PHP和AJAX製作了一個基於Web的單屏應用程序。我想將這個應用移植到iOS上,並開始自學Swift。我的應用程序在Swift 1中完美運行,但是由於我更新了xCode 7.2和Swift 2,因此我可以在iOS 9上使用我的iPhone嘗試應用程序,但代碼完全無法使用。從這裏開始,我有了新的代碼,並且據我所知,它應該可以工作,但我得到了一個空的迴應。哪裏不對?Swift 2,解析JSON,空響應

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. 
    } 


} 

的想法是我是從數據庫中獲取我的數據,看看是否「成功」(一個布爾值的分貝值)返回1或0。如果它返回1,然後執行一個原因請看下頁如果沒有,則創建一條警告,提示用戶名和密碼不正確。

預先感謝您!

+0

但你的代碼不這樣做!它只是初始化變量。 –

+0

@ChanchalRaj我明白了,我找不到解決方案使其工作 - 我說我不明白如何從URL中檢索json數據 –

+0

什麼是您的'password_TextFieldAction'?它是一個按鈕點擊或觸摸textField == – Lee

回答

0
  1. 創建URL

    let url = NSURL(string: "http://google.com")

  2. 創建請求

    let request = NSMutableUrlRequest(url: url!)

  3. 添加數據和請求方法

    request.HTTPMethod = "POST" request.HTTPBody = "username=\(username text.text!)".dataUsingEncoding(NSUTF8StringEncoding)

  4. 發送連接

    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 
        } 
    }) 
    
+0

建議使用NSURLSession而不是NSURLConnection。 – ryantxr

+0

@ryantxr從php文件發送的編碼是JSON,雖然 –

0

使用NSURLConnectionDelegateAFNetworking聯網請求。以下是使用NSURLConnection的實現。

我爲你實施了LoginViewController班。 將以下loginButtonAction掛鉤到您的用戶界面上的「登錄/登錄/完成」按鈕。

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) 

} 
} 
+0

不幸的是,這會導致多個錯誤 –

+0

您可以將您的代碼發送到[email protected]嗎?讓我爲你解決它。 –