2016-04-18 26 views
0

我試圖驗證我的應用程序,當他們打開應用程序的用戶。我有一個啓動畫面,將用戶電子郵件發送到服務器,並根據服務器的響應,它將加載主頁視圖或電子郵件驗證頁面。如何向服務器發送帖子,等待響應,然後根據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() 
     } 

的。

+0

你必須表現出對網絡呼叫完成處理的視圖。 –

回答

2

NSURLSession是當前支持的方式。使用NSURLSession您可以創建一個實現其委託方法的類來聲明它的委託,也可以使用塊/閉包。 C/Obj-C中的「塊」緊密映射到Swift中的「閉包」。它們允許你將函數作爲變量傳遞。您可以使用NSURLSession-dataTaskWithRequest:completionHandler:來完成此操作。

當加載完成時,completionHandler塊將被執行(在會話的委託隊列中)。從那裏,你可以採取行動。

下面是使用它(有一些注意事項)的一個簡單的例子:

let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { 
// Don't put other code before this next line, it is defining the closure we just started with the "{" 
(data, response, error) -> Void in 
    // This is where the closure body begins 
    print(NSThread.isMainThread()) // false (unless you've set the delegate queue to be the main queue) 

    // Example of processing the return value 
    let dataString = NSString.init(data: data!, encoding: NSUTF8StringEncoding) 
    print(dataString) // Okay to do work with this here, but don't do any UI work. 

    // Jump back over to the main queue to do any UI updates 
    // UI updates must always be done on the main queue 
    dispatch_async(dispatch_get_main_queue(), { 
     print(NSThread.isMainThread()) // true (we told it to execute this new block on the main queue) 
     // Execute the code to update your UI (change your view) from here 
     myCoolUIUpdatingMethod(dataString) 
    }); 

    // anything down here could be executed before the dispatch_async block completes 
    // dispatch_sync would block this thread until its block completes 
}); 

// run the task 
task.resume() 

// your program's main thread will not be blocked while the load is in progress 
+0

所以我把這個添加到我的代碼中,並沒有運行。我在「let任務」行之前和之後放置了一個print語句,並且在執行之前只放置了print語句。 –

+0

另外我在最後加上了一個task.resume(););「使任務實際運行。但它仍然是在觀點發生變化之後。 –

+0

我剛剛編輯我的帖子來澄清一些事情。如果您的第二個打印語句是我評論過的「不要在其下一行之前放置其他代碼...」,那就是它沒有運行的原因。這實際上在封閉的頭部內部,這是行不通的。如果您在「dispatch_async(dispatch_get_main_queue(),{」中有第二個打印以顯示它是主線程)的打印內容,那麼這是否有效? –

相關問題