http://[domain]/functionsdata.php?action=get_projectdetails This is my url how to do post city and how to fetch data based on city key in nsurlconnection in objective c ios?
I've made that code for my own project, but it should suit for your problem as well. This is the needed code to use it. You can add it to two separated files or the same file that is going to use it.
@implementation NSString (VMMStringWebStructure)
-(NSString*)stringToWebStructure
{
NSString* webString = [self stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
webString = [webString stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];
webString = [webString stringByReplacingOccurrencesOfString:@"?" withString:@"%3F"];
webString = [webString stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];
webString = [webString stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"];
return webString;
}
@end
@implementation NSWebUtilities
+(NSString*)launchURL:(NSURL*)url withPostString:(NSString*)post
{
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSError *error = nil;
NSHTTPURLResponse *response = nil;
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (response.statusCode >= 200 && response.statusCode < 300)
{
NSString *responseData = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
if (responseData.length > 0) return responseData;
}
else
{
if (error) return [error localizedDescription];
return [NSString stringWithFormat:NSLocalizedString(@"Received invalid status code: %d.",nil),response.statusCode];
}
return nil;
}
+(NSString*)launchURL:(NSURL*)url withPostValues:(NSDictionary*)postDict
{
NSArray* postKeys = postDict.allKeys;
NSMutableArray* postLines = [[NSMutableArray alloc] init];
for (NSString* key in postKeys)
{
[postLines addObject:[NSString stringWithFormat:@"%@=%@",key,[postDict[key] stringToWebStructure]]];
}
return [self launchURL:url withPostString:[postLines componentsJoinedByString:@"&"]];
}
@end
In your case, you could use it in that way:
NSString* urlString = @"http://[domain]/functionsdata.php?action=get_projectdetails";
NSURL *url = [NSURL URLWithString:urlString];
NSDictionary* postValues = @{@"city",@"<city name goes here>"};
NSString* output = [NSWebUtilities launchURL:url withPostValues:postValues];
That will retrieve the output has a NSString
called output
.