IOS/PHP: Capture success message in app after writing to server database -


after app inserts record in mysql database, capture id of new record , send app synching purposes.

the question how efficiently capture , send app.

here code posts server.

-(void) posttoserver: (nsstring *) jsonstring {     nsurl *url = [nsurl urlwithstring:@"http://www.~.com/services/new.php"];     nsmutableurlrequest *rq = [nsmutableurlrequest requestwithurl:url];     [rq sethttpmethod:@"post"];     nsdata *jsondata = [jsonstring datausingencoding:nsutf8stringencoding];    // nsdata *jsondata = [@"{ \"item\": \"hat\" }" datausingencoding:nsutf8stringencoding];     [rq sethttpbody:jsondata];      [rq setvalue:@"application/json" forhttpheaderfield:@"content-type"];     [rq setvalue:[nsstring stringwithformat:@"%ld", (long)[jsondata length]] forhttpheaderfield:@"content-length"];      [nsurlconnection sendasynchronousrequest:rq queue:[nsoperationqueue mainqueue] completionhandler:^(nsurlresponse *rsp, nsdata *data, nserror *err) {         nslog(@"post sent!");         nslog(@"error%@",err);     }]; } 

here php have set echo insert id.

$newid = mysql_insert_id(); echo $newid; 

however, how can have web service send info app?

thanks suggestions.

well echo json response (error/data) in php asynchronousrequest give response you:

[nsurlconnection sendasynchronousrequest:rq queue:[nsoperationqueue mainqueue] completionhandler:^(nsurlresponse *rsp, nsdata *data, nserror *err) {     nslog(@"post sent!");     if (err) {         nslog(@"error%@",err);     } else {         nsdictionary *jsonresults = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers error:nil];         nslog(@"results: %@", jsonresults);         // nsstring *insertid = jsonresults[@"insert_id"];     } }]; 

php:

// set response type json header("content-type: application/json");  ...     if ($sqlresult) {         // return insertid here         $response = array("insert_id" => $sqlresult['insert_id']); // example         exitwithresponse($response);     } else {         exitwithhttpcode(400, "bad request"); // example     } ...  # exit response function exitwithresponse($response) {     $status = array('code' => '200', 'response' => $response, 'error' => null);     echo json_encode($status);     http_response_code(200);     exit(); }  # exit error code function exitwithhttpcode($code, $error) {     $status = array('code' => $code, 'response' => null, 'error' => $error);     echo json_encode($status);     http_response_code($code);     exit(); } 

more status codes: http://www.w3.org/protocols/rfc2616/rfc2616-sec10.html


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -