2017-02-14 10 views
0

と通信した後、二回、それを押す必要があります。ユーザーが自分のアカウントを登録すると、mySQLデータベースに保存されます。だから私はログインをテストするために行く、私はログインを押して、何も起こらない、しかし、私は "成功"と言って私のPHPからのメッセージを受信して​​いるが、私は再びログインボタンをクリックするまで、アプリケーションは、おそらく私のif文が間違った場所にあると思ったかもしれませんが、私のtask.resumeがどこにあるかにロックされていて、if文がボタン・プレスで実行する必要があるため、どこを移動するのか分かりません。私はこれが修正するのは簡単な問題になると思うが、私の人生ではそれを理解することはできない。すべてのヘルプは高く評価され、これは私のloginViewController.swiftファイルです。(私はわざと私のPOSTのウェブサイトを削除)[スウィフト]私は私がログインを使用して、ページを登録するアプリケーションを作成していMySQLサーバ

import UIKit 

extension UIViewController { 
func hideKeyboardWhenTappedAround() { 
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard)) 
    view.addGestureRecognizer(tap) 
} 

func dismissKeyboard() { 
    view.endEditing(true) 
} 
} 



class loginViewController: UIViewController { 


struct globalVariable { 
    static var response = String() 

} 

@IBOutlet weak var emailTextField: UITextField! 

@IBOutlet weak var passwordTextField: UITextField! 



@IBAction func loginButtonTap(_ sender: Any) { 

    do { 
     let request = try NSMutableURLRequest(url: NSURL(string: "")! as URL) 
     request.httpMethod = "POST" 


     //Getting the values from the text fields and creating the post parameter 
     let postString = "email=\(emailTextField.text!)&password=\(passwordTextField.text!)" 


     //Adding the parameters to request body 
     request.httpBody = postString.data(using: String.Encoding.utf8) 

     //Creating a task to send the post request 
     let task = URLSession.shared.dataTask(with: request as URLRequest){ 
      data, response, error in 


      if error != nil{ 
       print("error is=\(error)") 
       return; 
      } 

      print("response = \(response)") 

      let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) 
      print("responseString = \(responseString)") 


      globalVariable.response = responseString as! String 

     } 
     task.resume() 

      if globalVariable.response.range(of: "true") != nil{ 

       var loginSwitch = self.storyboard?.instantiateViewController(withIdentifier: "tabBarController") as! UITabBarController 

       var appDelegate = UIApplication.shared.delegate as! AppDelegate 

       appDelegate.window?.rootViewController = loginSwitch 

     } 


    } 









} 















override func viewDidLoad() { 
    super.viewDidLoad() 

    self.hideKeyboardWhenTappedAround() 


} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 

} 

}

答えて

0

、そのままあなたは、完了ハンドラ内のWebサービスからの応答を処理する必要がありますWebサービスが返される前に、Webサービスからの応答を処理しようとしています。

//Creating a task to send the post request 
    let task = URLSession.shared.dataTask(with: request as URLRequest){ 
     data, response, error in 

     if error != nil{ 
      print("error is=\(error)") 
      return; 
     } 

     print("response = \(response)") 

     let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) 
     print("responseString = \(responseString)") 

     if responseString.range(of: "true") != nil{ 
      DispatchQueue.main.async { 
       var loginSwitch = self.storyboard?.instantiateViewController(withIdentifier: "tabBarController") as! UITabBarController 
       var appDelegate = UIApplication.shared.delegate as! AppDelegate 
       appDelegate.window?.rootViewController = loginSwitch 
      } 
     } 
    } 
    task.resume() 
+0

Iveはif文を実行する前にtask.resumeを呼び出す必要があるため、エラーが発生しています。これは、エラーのlibC++ abi.dylibを撮影:タイプNSException (lldb)のキャッチされない例外で終了すると0 __pthread_killに私を取り、スレッド10を言う:信号SIGABRT – CodingHelpPls

+0

は私の更新の答えをしようと、私は完了ハンドラが実行されているのを忘れバックグラウンドスレッドは、完了ハンドラで直接UIの変更を行うことはできませんので、そのロジックをメインスレッドに戻す必要があります。 – Fonix

関連する問題