objective c - Uploading image to server on IOS device -



objective c - Uploading image to server on IOS device -

i using nsmutableurlrequest send image server. able read request.body.image on server side when seek opening picture, unable (i opening uploading amazon s3 server , making public). here obj c code:

- (bool)sendprolilepic: (uiimage*)image { //we need email , password request nsstring *password = [self.keychainitem objectforkey:(__bridge id)(ksecvaluedata)]; nsstring *email= [self.keychainitem objectforkey:(__bridge id)(ksecattraccount)]; // create request. nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:[nsurl urlwithstring:@"http://localhost:3000/set_profilepic"]]; // specify post request request.httpmethod = @"post"; // how set header fields [request setvalue:@"application/x-www-form-urlencoded; charset=utf-8" forhttpheaderfield:@"content-type"]; //make mutable info store info nsmutabledata *requestbodydata = [nsmutabledata data]; // add together loggin info , image requestbodydata nsstring *stringdata = [nsstring stringwithformat:@"email=%@&password=%@&image=",email,password]; [requestbodydata appenddata:[stringdata datausingencoding:nsutf8stringencoding]]; // nsdata *imagedata = uiimagepngrepresentation(image);//convert image [requestbodydata appenddata:[nsdata datawithdata: uiimagejpegrepresentation(image, 1.0)]];//append image request.httpbody = requestbodydata; // create url connection , fire request nsurlresponse * response = nil;//the raw response stored in variable nserror * error = nil;//if error occurs stored in variable //send http post request server nsdata * info = [nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&error]; //convert server response string nsstring *stringfromdata = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding]; //parse http response , extract statuscode nshttpurlresponse *httpresponse = (nshttpurlresponse *)response; nsinteger statuscode = [httpresponse statuscode]; if (error == nil)//if there no error (server reachable) { if(statuscode == 200){ if(error){ [self displayalertwithtitle:@"error" content:@"looks did stupid , we're trying our best prepare it. give thanks patience"]; homecoming false; }else{ homecoming true;//if profile image has been uploaded, homecoming true. otherwise homecoming false } }else if(statuscode == 403){ //user doing wrong //create uiview controller , display error message [self displayalertwithtitle:@"error" content:stringfromdata]; homecoming false; }else{ //something wrong , don't know //create uiview controller , display generic error message [self displayalertwithtitle:@"error" content:@"server not responding. please seek 1 time again later"]; homecoming false; } }else{ //either server downwards or user's net connection downwards [self displayalertwithtitle:@"error" content:@"please check net connection. might updating our servers. please seek 1 time again later"]; homecoming false; }}

i new objective c. can check if i'm making obvious mistake?

ios objective-c

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -