我試圖驗證我的應用程序,當他們打開應用程序的用戶。我有一個啓動畫面,將用戶電子郵件發送到服務器,並根據服務器的響應,它將加載主頁視圖或電子郵件驗證頁面。如何向服務器發送帖子,等待響應,然後根據Swift中的響應更改視圖?
我得到的問題是,當我發佈帖子時,它似乎把它放在它自己的單獨線程上,直到視圖已經更改並且應用程序已解決,我纔看到響應。
我曾嘗試使用:
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())
和
let task = session.dataTaskWithRequest(request)
{
//more stuff to handle return
}
task.resume()
也
NSURLConnection.sendSynchronousRequest()
與第一和第三個選項的問題是,他們是不贊成的方法,在除了他們在自己的線程上工作,並在if語句改變視圖之後接收到響應已經跑了。
這是正在使用的代碼片段:if語句來改變視角是基於服務器的文件夾中的用戶文件存儲的響應
//returns the email of the user
let email = self.fileCom.getUserStringField("email")
//write the string to post to the server
let post = "email=" + email +
"&pass=" + self.serverCom.PASSWORD
//post the user email to verification script and log result
logResponse(self.serverCom.postToServer(self.serverCom.getVerifyEmailScriptURL(), bodyData: post))
//Check the status of verified and email
// if(self.fileCom.getUserBoolField("verified"))
if(false)//temp
{
//change view to home view
print("\nHomeView\n")
showHomeView()
}
else
{
//change view to verification view
print("\nVerifyView\n")
showVerificationView()
}
的。
你必須表現出對網絡呼叫完成處理的視圖。 –