iOS SDK : Customers creation via iOS SDK


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 user, options: [])
    } catch {
        print("Error: cannot create JSON from todo")
    let request = NSMutableURLRequest(url: NSURL(string: "")! 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)

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

1 Like

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


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)”


1 Like

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:


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