My code:
NSMutableArray *sstr = [[DBModel database]getCName];
NSArray *aarr = [[sstr objectAtIndex:0] componentsSeparatedByString:@"!"];
acName = [aarr objectAtIndex:0];
acMobileno = [aarr objectAtIndex:1];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http:///userstatus.php?name=%@&mobileno=%@&status=off",[aarr objectAtIndex:0],[aarr objectAtIndex:1]]];
NSMutableURLRequest *postRequest = [[NSMutableURLRequest alloc]initWithURL:url];
[postRequest setHTTPMethod:@"POST"];
NSString *stringBoundary = @"------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
[postRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postBody = [NSMutableData data];
//name
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"name\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"%@",[aarr objectAtIndex:0]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
//mobileno
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"mobileno\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"%@",[aarr objectAtIndex:1]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
//status
NSString *status = @"off";
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"status\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"%@",status] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postRequest setHTTPBody:postBody];
NSData *returndata=[NSURLConnection sendSynchronousRequest:postRequest returningResponse:nil error:nil];
NSString *string1=[[NSString alloc]initWithData:returndata encoding:NSUTF8StringEncoding];
NSLog(@"OFF Status report = %@",string1);
I am running this code with performSelectorInBackground:
in didEnterBackground
method.
Sometimes it gives me the strange problem of not updating the table in server.
Is it problem with using performSelector
in delegate method or should i change this request to asynchronous?