-(void)getMeetings
{
NSString *requestURL = [NSString stringWithFormat:@"%@",@"someurl"];
[self webserviceCreate:nil urlOfwebservice:[NSURL URLWithString:requestURL] tag:1];
}
-(void)webserviceCreatePost:(NSDictionary *)dict urlOfwebservice:(NSURL *)url tag:(int)tag
{
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
options:NSJSONWritingPrettyPrinted
error:&error];
NSString *requestJson = @"";
if (!jsonData) {
//Deal with error
} else {
requestJson = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
NSLog(@"jsonRequest is %@", requestJson);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
connectionToInfoMapping = CFDictionaryCreateMutable(kCFAllocatorDefault,0,&kCFTypeDictionaryKeyCallBacks,&kCFTypeDictionaryValueCallBacks);
NSData *requestData = [requestJson dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:[[NSUserDefaults standardUserDefaults]valueForKey:@"SessionKey"] forHTTPHeaderField:@"Authorization"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody: requestData];
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
CFDictionaryAddValue(connectionToInfoMapping,(__bridge const void *)(connection),
(__bridge const void *)([NSMutableDictionary
dictionaryWithObjectsAndKeys:[NSMutableData data],@"receivedData",[NSString stringWithFormat:@"%d",tag],@"tag", nil]));
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// [receivedData setLength:0];
NSMutableDictionary *connectionInfo = CFDictionaryGetValue(connectionToInfoMapping, (__bridge const void *)(connection));
receivedData = [connectionInfo objectForKey:@"receivedData"];
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSMutableDictionary *connectionInfo = CFDictionaryGetValue(connectionToInfoMapping, (__bridge const void *)(connection));
[[connectionInfo objectForKey:@"receivedData"] appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[HUD hide:YES];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:[error localizedDescription] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSMutableDictionary *connectionInfo = CFDictionaryGetValue(connectionToInfoMapping, (__bridge const void *)(connection));
int tag = [[connectionInfo valueForKey:@"tag"] intValue];
if (tag == 1)
{
NSArray *arrMeeting = [NSJSONSerialization JSONObjectWithData:[connectionInfo valueForKey:@"receivedData"] options:0 error:nil];
}
}