User Login with Swift


#1

Hi all,

Apologies if I have missed this, but I’ve done a quick search and couldn’t find a solid example. Has anyone managed to write anything to log into Dreamfactory (or even better, user registration) that uses Swift. I’ve tried with a combination of NSURL and AlamoFire but been unsuccessful as I get an error {“error”:{“context”:null,“message”:“Unrecognized payload type”,“code”:400}

Code:

    let uri = NSURL(string: "\(baseUserUrl)session")!
    
    let mutableURLRequest = NSMutableURLRequest(URL: uri)

    let manager = Alamofire.Manager.sharedInstance
    manager.session.configuration.HTTPAdditionalHeaders = ["X-DreamFactory-Api-Key":"xxx"]
    manager.request(.POST,mutableURLRequest,parameters:["email":"xxx@xxx.xxx","password":"xxx","duration":0]).response { request, response, data, error in
        print(request)
        print(response)
        print(NSString(data: data!, encoding: NSUTF8StringEncoding))
    }

Kind Regards

Gavin


#2

Here is the login example from the iOS example app, but I think it’s written in objective C rather than the new Swift:

NSString *serviceName = @"user";
NSString *resourcePath = @"session";
NSString *restApiPath = [NSString stringWithFormat:@"%@/%@/%@", baseUrl, serviceName, resourcePath];
NSDictionary* requestBody = @{@"email":self.emailTextField.text,
                              @"password":self.passwordTextField.text};

Since I assume that doesn’t help, I have moved this topic to the Code Examples category so that hopefully someone in the community who’s using Swift can chime in with a snippet of their app.