2016-04-18 8 views
0

アプリを開くときに自分のアプリのユーザーを確認しようとしています。私はユーザーのメールの投稿をサーバーに送信するスプラッシュ画面を持っており、サーバーからの応答に基づいてホームビューまたは電子メール確認ページを読み込みます。どのようにサーバーに投稿を行い、応答を待ってから、Swiftの応答に基づいてビューを変更しますか?

私が得た問題は、投稿を作成すると、それを別のスレッドに入れているように見えて、ビューが既に変更されてアプリが解決するまでレスポンスが表示されないことです。

私が使用しようとしました

NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) 

 let task = session.dataTaskWithRequest(request) 
     { 
     //more stuff to handle return 
     } 
      task.resume() 

とも

NSURLConnection.sendSynchronousRequest() 

第一および第三のオプションを使用して問題がで、彼らは方法が廃止されていることです彼ら自身のスレッドで作業することに加えて、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の「Closure」に密接に対応しています。変数を変数として扱うことができます。これを行うには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 task"行の前後にprint文を置き、実行前にprint文だけを置いています。 –

+0

また、最後の ");の後にtask.resume()を追加しました。タスクを実際に実行させる。しかし、それはまだビューが変化した後です。 –

+0

いくつかのことを明確にするために投稿を編集しました。 2番目のprintステートメントが「次の行の前に他のコードを置いてはいけません...」とコメントした場所だったので、それが実行されなかったのです。これは効果的にクロージャのヘッダーの内側にありますが、これは機能しません。 "dispatch_async(dispatch_get_main_queue()、{"はメインスレッドであることを示すために2番目のプリントがありますが、それは動作します) –

関連する問題