等待异步调用完成

时间:2016-01-20 19:24:07

标签: ios swift

在viewcontroller的加载时我禁用了几个元素。此后视图控制器正在进行AFNetworking POST。根据结果​​,控制需要改变控制器的状态。但根据下面的代码,我无法处理这件事。

//调用网络调用的类方法的控制器代码

 override func viewDidLoad() {
        super.viewDidLoad()

        btnNext.hidden = true;
        txtEmail.enabled = false;
        if APISFAuthentication.sfAuthenticateGuest() {

            btnNext.hidden = false
            txtEmail.enabled = true
            vProgressIndicator.hidden = true
        }


    }

//类定义如下

import Foundation

class APISFAuthentication {


  init(x: Float, y: Float) {

  }

  static func sfAuthenticateGuest() -> Bool {

    var isConneced = false
    let manager = AFHTTPRequestOperationManager()
    let postData = ["grant_type":"password","client_id":APISessionInfo.SF_CLIENT_ID,"client_secret":APISessionInfo.SF_CLIENT_SECRET,"username":APISessionInfo.SF_GUEST_USER,"password":APISessionInfo.SF_GUEST_USER_PASSWORD]

    manager.POST(APISessionInfo.SF_APP_URL,
        parameters: postData,
        success: { (operation, responseObject) in
            print("JSON: " + responseObject.description)
            isConneced = true
        },
        failure: { (operation, error) in
            print("Error: " + error.localizedDescription)

    })
    return isConneced
  }

  static func sfAuthenticateUser(userEmail: String,userPassword: String) -> Bool {


    let manager = AFHTTPRequestOperationManager()
    let postData = ["grant_type":"password","client_id":APISessionInfo.SF_CLIENT_ID,"client_secret":APISessionInfo.SF_CLIENT_SECRET,"username":APISessionInfo.SF_GUEST_USER,"password":APISessionInfo.SF_GUEST_USER_PASSWORD]

    manager.POST(APISessionInfo.SF_APP_URL,
        parameters: postData,
        success: { (operation, responseObject) in
            print("JSON: " + responseObject.description)
        },
        failure: { (operation, error) in
            print("Error: " + error.localizedDescription)

    })
    return true;
  }
}

0 个答案:

没有答案