Import pod ‘Moya’, Github address

  • Moya is a secondary package of Alamofire

Code demo

// Example of guardlet username = usernameTF.text else {
            print("Account cannot be empty.")
            return
        }
        LGLoginClient.manager.smscode(username: username) { [weak self](smscode) inself? . SmscodeTF. Text = smscode} / / second instance LGLoginClient manager. Login (username: usernameTF text! , password: passwordTF.text! , smscode: smscodeTF.text!)Copy the code
  • agreementTargetType
//LGLoginAPI.swift
import UIKit
import Moya

public enum LGLoginAPI {
    caseLogin (String, String, String) // Login interfacecaseSmscode (String) // Log in and send verification codecase} extension LGLoginAPI: TargetType {var baseURL: URL {return URL(string:"http://127.0.0.1:5000/")! } public var path: String {switch self {case .login:
            return "login/"
        case .smscode:
            return "login/smscode/"
        case .otherRequest:
            return "login/otherRequest/"}} public var method: moya. method {switch self {case .login:
            return .post
        case .smscode:
            return .post
        default:
            returnPublic var sampleData: Data {public var sampleData: Data {return "{}".data(using: String.Encoding.utf8)! } public var task: task {var param:[String:Any] = [:] switch self {case .login(let username,let password,let smscode):
            param["username"] = username
            param["password"] = password
            param["smscode"] = smscode
        case .smscode(let username):
            param["username"] = username
        default:
            return .requestPlain
        }
        return.requestParameters(parameters: param, encoding: urlencoding.default)} public var headers: [String: String]? {return nil
    }
    
 
}
Copy the code
  • MoyaProviderThe initiating
//LGLoginClient.swift
import UIKit
import Moya

class LGLoginClient: NSObject {
    static letManager = LGLoginClient () / / MARK: - log events func login (username: String, password: String, smscode: String) {let provide = MoyaProvider<LGLoginAPI>()
        provide.request(.login(username, password, smscode)) { (result) in
            switch result{
            case let .success(response):
                let _ = LGLoginClient.lgJson(data: response.data)
            case let .failure(error):
                print(error)}}} / / MARK: - captcha events func smscode (username: String, complete: @ escaping ((String) - > Void)) {let provide = MoyaProvider<LGLoginAPI>()
        provide.request(.smscode(username)) { (result) in
            switch result{
            case let .success(response):
                let dict = LGLoginClient.lgJson(data: response.data)
                complete(dict["smscode"] as! String)
            case let .failure(error):
                print(error)
                complete("")}}} //MARK: - Other events - such as registering funcotherRequest() {
        let provide = MoyaProvider<LGLoginAPI>()
        provide.request(.otherRequest) { (result) in
            switch result{
            case let .success(response):
                let _ = LGLoginClient.lgJson(data: response.data)
            case let .failure(error):
                print(error) } } } static func lgJson(data:Data?) ->([String: Any]){ guardlet data = data else {
            print("The data is empty")
            return[:]}do {
            let dict = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
            print("Serialized dictionary: dict")
            return dict as! ([String : Any])
        } catch {
            print("Serialization failed")
            return[:]}}}Copy the code