0

I have been implementing login framework to use in app development. I have created login framework functionality and it works fine.

Now i'm trying to do callback of return response of whether it success or failed from app to framework wise versa framework to app.

here is my framework code:

   public func loginApiCall(username: String?, password: String?) {


    let parameters = [
        "username": username,
        "password": password
        ]
    print(parameters)

    let url = "apiUrl/authentication/"
    Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: [:]).responseJSON {
        response in
        switch (response.result) {
        case .success:
            print(response)
            break
        case .failure:
            print(Error.self)
        }
    }

  }

Here is my sampleapp where i have using myframework:

    override func viewDidLoad() {
    super.viewDidLoad()

    //framework called here...
    let apiCall = APICall.init()
    apiCall.loginApiCall(username: "demo", password: "demo")

}//viewdidload
PvUIDev
  • 95
  • 1
  • 9
  • 38
  • what's the issue, take a completion block as argument to your `loginApiCall` function and execute it with response to handover response to app – Sandeep Bhandari Nov 06 '19 at 06:32

2 Answers2

1

you can achieve using callback

// framework code

public func loginApiCall(username: String?, password: String?, callback : @escaping ((Bool) -> Void)) {

    let parameters = [
        "username": username,
        "password": password
    ]
    print(parameters)

    let url = "apiUrl/authentication/"
    Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: [:]).responseJSON {
        response in
        switch (response.result) {
        case .success:
            callback(true)
        case .failure:
            callback(false)
        }
    }

}

// viewDidLoad code

 override func viewDidLoad() {

super.viewDidLoad()
//framework called here...
let apiCall = APICall.init()
    apiCall.loginApiCall(username: "demo", password: "demo") { (status) in
        print("code")
    }

}

//NOTE : create completionHandler as per your requirement, it is a demo completionHandler

SGDev
  • 2,256
  • 2
  • 13
  • 29
  • There is no need for `break` in `success` case. `break` is by default added. – PGDev Nov 06 '19 at 06:36
  • @SGDev whats difference between callback and delegate – PvUIDev Nov 06 '19 at 06:37
  • its shows an error Escaping closure captures non-escaping parameter 'callback' in framework – PvUIDev Nov 06 '19 at 06:42
  • check my updated code, add @escaping keyword – SGDev Nov 06 '19 at 06:44
  • How to return response data to sample app – PvUIDev Nov 06 '19 at 06:52
  • how can i send response data SUCCESS: { authToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhOGZsb3ciLCJleHAiOjE1NzMxOTY2MjUsInVzZXJuYW1lIjoiZGVtbyJ9.HvNpPPIWeM5-HuZ7wJl3JWIAbIlz-ktvfsRlIl6OlGE"; expireDate = "Fri Nov 08 07:03:45 UTC 2019"; groups = ( accounting, "camunda-admin", management, sales ); username = demo; } of authToken, username to sample app when response is success. – PvUIDev Nov 06 '19 at 07:04
  • @SGDev its all time going to true case if i given wrong password it does not print failure case error, it shows only true.. – PvUIDev Nov 06 '19 at 07:20
0

first you have to define completion handler, after then you can use it inside of your function parameter. I suggest you to check out completions to achieve that easily.

typealias DownloadComplete = () -> ()

public func loginApiCall(username: String?, password: String?, completed: @escaping DownloadComplete)) { let parameters = [ "username": username, "password": password ] print(parameters)

let url = "apiUrl/authentication/"
Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: [:]).responseJSON {
    response in
    switch (response.result) {
    case .success:
        completed().  // important part
    case .failure:

    }
}
Bartu Akman
  • 187
  • 10