iOS SDK : Customers creation via iOS SDK

#1

Hi :slight_smile:

I used this code to create a new customer , but i can’t found it in my dashboard. what am I doing wrong please ?

static public func createCustomer(userName: String, userEmail: String, password: String) {
    let headers = [
        "Accept": "application/json",
        "Content-Type": "application/json",
        "Authorization": "Bearer \(moltin.config.clientID)"
    ]
    let user = ["data": [
        "type": "customer",
        "name": userName,
        "email": userEmail,
        "password": password
        ]] as [String : Any]
    
    let userData: Data
    do {
        userData = try JSONSerialization.data(withJSONObject: user, options: [])
    } catch {
        print("Error: cannot create JSON from todo")
        return
    }
    
    let request = NSMutableURLRequest(url: NSURL(string: "https://api.moltin.com/v2/customers")! as URL,cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0)
    request.httpMethod = "POST"
    request.allHTTPHeaderFields = headers
    request.httpBody = userData
    
    let session = URLSession.shared
    let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
        if (error != nil) {
            print(error as Any)
        } else {
            let httpResponse = response as? HTTPURLResponse
            print(httpResponse as Any)
        }
    })
    
    dataTask.resume()
}
#2

Taking a look now. I will get back to you.

1 Like
#3

Thank you , I’ll be waiting for you :blush:

#4

Yes, i see the issue. I also suspect this is from your example app. Sorry, I will get a fix for this an pushed today.

“Authorization”: “Bearer (moltin.config.clientID)”

-George

1 Like
#5

Okey Thank you for your help :slight_smile: , please give me a sign when you fix the issue.
I have one more question please : How can i get the access token ? There is a method called “func authenticate” under “MoltinAuth” class that return Result<(token: String?, expires: Date?)> , i can’t found out how to call this method :pensive:

#6

Hey Bouthaina,

I sorted it out in a simple way that does not require any SDK changes. You are right those class are internal so not able to access.

I think the best way may be to pull the token from user settings. It should be keep up to data as long as you are making other API calls.

I wanted to get you a solution as soon as possible. I am going spend some more time thinking about this. Do let me know if you hit any issues with this.

        var token: String = ""
         struct moltinToken: Codable {
            var clientID: String
            var token: String
            var expires: Date
        
        }
        if let data = UserDefaults.standard.value(forKey: "Moltin.auth.credentials") as? Data {
            let credentials = try? JSONDecoder().decode(moltinToken.self, from: data)
            token = credentials?.token ?? ""
        }
        let headers = [
            "Accept": "application/json",
            "Content-Type": "application/json",
            "Authorization": "Bearer \(token)"
        ]
1 Like