2016-06-12 7 views
-1

私はWebページに「ログイン」するいくつかの行を持っており、内容を取り出してコンソールに出力しますが、「タスク」から結果を得る方法を理解することはできません。後でコード内で使用してください。Swiftにログインした後のURLの内容を返しますか?

let request = NSMutableURLRequest(URL: NSURL(string: "http://localhost:8888/mobilelogin.php")!) 
    request.HTTPMethod = "POST" 
    let username = email_input.text; 
    let password = password_input.text; 
    var postString = "username=" 
    postString += username! 
    postString += "&password=" 
    postString += password! 
    print(postString); 
    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding) 
    print(request.HTTPBody); 
    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in guard error == nil && data != nil 
     else { 
      // check for fundamental networking error 
      print("error=\(error)") 
      return 
     } 

     if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 { 
      // check for http errors 
      print("statusCode should be 200, but is \(httpStatus.statusCode)") 
      print("response = \(response)") 
      return 
     } 

     let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)! 
     print("responseString = \(responseString)"); 
     return 

    } 
    print("This is the task string") 
    task.resume() 

答えて

0

クロージャから戻ることはできません。コールバックを使用する必要があります。

我々はあなたのコードのために機能します:

func getData(username username: String, password: String) 

ではなく、戻り値の型を追加するのを、私たちは、ここに名前の「完了」コールバックを追加します。

func getData(username username: String, password: String, completion: (response: String)->()) { 

} 

そして、関数内

、データが利用できる場所でこのコールバックを使用します:

func getData(username username: String, password: String, completion: (response: String)->()) { 
    let request = NSMutableURLRequest(URL: NSURL(string: "http://localhost:8888/mobilelogin.php")!) 
    request.HTTPMethod = "POST" 
    var postString = "username=" 
    postString += username 
    postString += "&password=" 
    postString += password 
    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding) 
    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in 
     guard let data = data where error == nil else { 
      fatalError(error!.debugDescription) 
     } 

     if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 { 
      print("response = \(response)") 
      fatalError("statusCode should be 200, but is \(httpStatus.statusCode)") 
     } 

     guard let str = String(data: data, encoding: NSUTF8StringEncoding) else { 
      fatalError("impossible to get string from data") 
     } 

     completion(response: str) 

    } 
    task.resume() 
} 

e:

getData(username: email_input.text!, password: password_input.text!) { (response) in 
    print(response) 
} 
+0

ありがとうございます! – inigomontoya333

+0

変数として取得する方法はありますか?ラベルを設定するには? – inigomontoya333

+0

ようこそ。 [this](http://stackoverflow.com/help/someone-answers)を忘れないでください。あなたの変数のために:もちろん! :)あなたは何をしたいのですか? 'print(response)'の代わりに 'getData'では、必要に応じてラベルなどのためにレスポンスを使います。 – Moritz

関連する問題