Fawaz Faiz Fawaz Faiz - 1 month ago 14
iOS Question

How to send selected values of JSON Data to server from an uitableview by tapping a button

I implemented a method to load

JSON
Data to a
UITableView
and passed the multiple selected Array and loaded it in another tableview which is in the same
UITableView
please refer this to understand my project.

What I want is to send the selected
JSON
data from Tableview2 to the server on tapping the confirm button.

I received the following url format as confirmed url (Only DNS name is edited because of privacy. Just take a look at the url format)

http://satests.com/srm/php/AJAXHandler.php?command={ "object": "BusinessEntity", "method": "SetSQLView", "params": { "name": "InsertWPLog", "records": [ { "worksitepersonnelid": "35a0c9e1XX-161d-11e6-804c-005056bc570d", "actiondate": "2016-10-06 13:24:35", "actiontype": "SIGNIN" } ] }, "security": { "userID": 2 } }


A Java Developer from my team sent me a method to send this url in java as follows :

// button on click event
String url = "http://satests.com/srm/php/AJAXHandler.php?command={\"object\": \"BusinessEntity\",\"method\":\"SetSQLView\",\"params\":{\"name\":\"InsertWPLog\",\"records\":[";
String date = ""; //get system date
for(String userID : userIds) {
url = url + "{\"worksitepersonnelid\":\"" + userID + "\",\"actiondate\": \""+date+"\",\"actiontype\": \"SIGNIN\"}";
}
url = url + "]},\"security\":{\"userID\": 2}}";

// send request url


I don't understand anything from this but I wanted to send the above url format to the server.

Can Anyone explain the above method or post sample code for confirm button click event in OBjective-C.

Answer

Something like this should do the tick. You'll need to format the date string however you want it and change the request to GET if it's not a POST request.

NSString *urlString = @"http://satests.com/srm/php/AJAXHandler.php?command={\"object\": \"BusinessEntity\",\"method\":\"SetSQLView\",\"params\":{\"name\":\"InsertWPLog\",\"records\":[";
NSString *date; //I'm not sure how you want this formatted.

for (NSString *userID in userIds) {
    urlString = [urlString stringByAppendingString:[NSString stringWithFormat:@"{\"worksitepersonnelid\":\"%@\",\"actiondate\": \"%@\",\"actiontype\": \"SIGNIN\"}", userID, date]];
}
urlString = [urlString stringByAppendingString:@"]},\"security\":{\"userID\": 2}}"];


NSURL *url = [NSURL URLWithString:urlString];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];

[request setHTTPMethod:@"POST"]; //Change to 'GET' if the request is 'GET'.

NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
    if (!error) {
        //Sucessful
    }else {
        //Error
        NSLog(@"URL Session Error: %@", error);
    }
}];
[task resume];
Comments