iOS client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#import "CreateUserConnection.h" #import "FlowRange.h" @interface CreateUserConnection () <NSURLConnectionDelegate> { } @property (nonatomic, strong) NSMutableData *buffer; @property (nonatomic, strong) NSURLConnection *connection; //block that gets retained when posting to WS, then used when the connection is finished @property (nonatomic, copy) OnUserCreatedCloud completed; @property (nonatomic, copy) NSString * auth_token; @property (nonatomic, copy) NSString * email; @property (nonatomic, assign) NSString* userID; @property (nonatomic, copy) NSString * jsonInputString; @property (nonatomic, strong) NSDictionary * info; @property (nonatomic, strong) NSDictionary * user; @end @implementation CreateUserConnection #pragma mark - ------------- LifeCycle ------------- -(instancetype)initWithEmail:(NSString*)email andAuthToken:(NSString*)token andID:(NSString*)newID { if(self=[super init]) { self.email = email; self.auth_token = token; self.userID = newID; FlowRange * range = [[FlowRange alloc] initWithBeginDate:today andEndDate:today]; NSArray * array = [NSArray arrayWithObjects:[self stringFromNSDate:range.beginDate], [self stringFromNSDate:range.endDate], @"08-03-2015", @"08-10-2015", nil]; //build an info object and convert to json self.info = [NSDictionary dictionaryWithObjectsAndKeys: self.email, @"name", @"12345678", @"password", array, @"flowDates", nil]; NSError *error = nil; //1st step NSData *jsonInputData = [NSJSONSerialization dataWithJSONObject:self.info options:NSJSONWritingPrettyPrinted error:&error]; //2nd step self.jsonInputString = [[NSString alloc] initWithData:jsonInputData encoding:NSUTF8StringEncoding]; } return self; } |
Basically, as we’re constructing our dictionary, our key is flowDates. We use an array of strings as our value.
1 2 3 4 5 |
self.info = [NSDictionary dictionaryWithObjectsAndKeys: self.email, @"name", @"12345678", @"password", array, @"flowDates", nil]; |
Server side
The json packet comes to our server via the request object.
In the request object’s body, we use key flowDates to get the array of strings passed in from the iOS client. We loop through this flowDates array to get the strings.
While we loop, we create a temp object called flowDay with keys startDate and endDate, and enter the strings from the array flowDates as their value.
1 2 |
var flowDay = { startDate: req.body.flowDates[i] , endDate: req.body.flowDates[i+1] }; |
Then we push that object onto an empty array.
1 |
flowDaysArray.push(flowDay); |
Finally, we use that array to create a new user:
1 2 3 4 5 6 7 |
// create a sample user var nick = new User({ name: req.body.name, password: req.body.password, //You would protect your passwords by hashing it. admin: true, flowDates: flowDaysArray }); |
Server side code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
app.post('/setup', function(req, res) { console.log('POST parameter received, name is: ' + req.body.name); var flowDaysArray = []; //req.body.flowDates is an array of strings coming in for ( var i = 0; i < req.body.flowDates.length; i+=2) { //create flowDates object, and put these strings in var flowDay = { startDate: req.body.flowDates[i] , endDate: req.body.flowDates[i+1] }; console.log( i + ', flow start day is: ' + flowDay.startDate + ', flow end day is: ' + flowDay.endDate); flowDaysArray.push(flowDay); } // create a sample user var nick = new User({ name: req.body.name, password: req.body.password, //You would protect your passwords by hashing it. admin: true, flowDates: flowDaysArray }); // save the sample user nick.save(function(err) { try { if (err) throw err; console.log('User ' + req.body.name + ' saved successfully...!'); res.json({ success: true }); } catch(err) { console.log(err); } }); }); |