Hi developers, im doing a framework with this function
public func LoginGenerateToken(userName: String, password: String) {
let parameters = LoginGenerateTokenStruct(userName: userName, password: password.sha256())
LoginSingleton.shared.mailUserLogin = userName
networkManager1 = URL_Session()
networkManager1?.delegate = self
networkManager1?.LoginGenerateTokenPOST(parameters: parameters)
}
so when the user install the framework and call this function it should ask for 'userName' and 'password'
but it doesn't because is not an static function
the function get call like this
LoginWS.LoginGenerateToken()
insted of
LoginWS.LoginGenerateToken(userName: String, password: String)
i can't set static because im calling a delegate outside the func LoginGenerateToken
networkManager1?.delegate = self
how could i get the function to work without using static?
this is the full code
//
// LoginWS.swift
// IntegradorFrameWork
//
// Created by BlumonPay on 11/27/19.
// Copyright © 2019 BlumonPay. All rights reserved.
//
import Foundation
import CommonCrypto
public class LoginWS: URL_SessionDelegate{
var networkManager1: URL_Session?
func connectionFinishWithError(session: URL_Session, error: Error) {
print(error)
}
func connectionFinishSuccessfull(session: URL_Session, response: NSDictionary) {
print(response)
let dictionaryValue = response as? NSDictionary
let responseError = dictionaryValue?["error"] as? String
let errorDescription = dictionaryValue?["error_description"] as? String
if responseError?.count == nil {
let dictionaryValue = response as? NSDictionary
let tokenAcceso = dictionaryValue?["access_token"] as! String
print("Access Token: \(tokenAcceso)")
}else{
let responseError = dictionaryValue?["error"] as! String
let errorDescription = dictionaryValue?["error_description"] as! String
print("ERROR: \(errorDescription)")
}
}
public func LoginGenerateToken(userName: String, password: String) {
let parameters = LoginGenerateTokenStruct(userName: userName, password: password.sha256())
LoginSingleton.shared.mailUserLogin = userName
networkManager1 = URL_Session()
networkManager1?.delegate = self
networkManager1?.LoginGenerateTokenPOST(parameters: parameters)
}
}
extension String {
func sha256() -> String{
if let stringData = self.data(using: String.Encoding.utf8) {
return hexStringFromData(input: digest(input: stringData as NSData))
}
return ""
}
private func digest(input : NSData) -> NSData {
let digestLength = Int(CC_SHA256_DIGEST_LENGTH)
var hash = [UInt8](repeating: 0, count: digestLength)
CC_SHA256(input.bytes, UInt32(input.length), &hash)
return NSData(bytes: hash, length: digestLength)
}
private func hexStringFromData(input: NSData) -> String {
var bytes = [UInt8](repeating: 0, count: input.length)
input.getBytes(&bytes, length: input.length)
var hexString = ""
for byte in bytes {
hexString += String(format:"%02x", UInt8(byte))
}
return hexString
}
}
delegate method
@objcprotocol URL_SessionDelegate {
func connectionFinishSuccessfull(session: URL_Session, response: NSDictionary) //Response del mismotipo que devuelve el JSON o utilizar any para cachar cualquiera de los 2
func connectionFinishWithError(session: URL_Session, error: Error)
@objc optional func connectionFinishSuccessfull2(session: URL_Session, response: NSDictionary)
@objc optional func connectionFinishSuccessfull3(session: URL_Session, response: NSDictionary)
@objc optional func connectionFinishSuccessfull4(session: URL_Session, response: NSDictionary)
}
class URL_Session: NSObject, URLSessionDelegate, URLSessionDataDelegate {
var dataTask: URLSessionDataTask? //Descarga los bytes, evalua la respuesta del servidor
var responseData: Data = Data() //Respuesta del lado del servidor
var httpResponse: HTTPURLResponse?
static var delegate: URL_SessionDelegate?
var delegat2: URL_SessionDelegate?
var delegat3: URL_SessionDelegate?
var delegat4: URL_SessionDelegate?