-1

Accounting for the various iOS devices and orientations. How to make webservice name in tableview pass the data in services?

(void)callWebserviceWithNSMutableRequest
{
    [appDelegate showGlobalProgressHUDWithTitle:@"Loading.."];

    NSURL *url = [NSURL URLWithString:@"https://www.tipspinz.com/app/api/state_list.php"];

    //    NSDictionary *postDict = [NSDictionary dictionaryWithObjectsAndKeys:txtFirstname.text, @"firstname",
    //                              txtLastname.text, @"lastname", nil];

    NSDictionary *postDict = [NSDictionary dictionaryWithObjectsAndKeys:@"", @"", nil];
    NSData *postData = [NSKeyedArchiver archivedDataWithRootObject:postDict];

    // Create the request
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:[NSString stringWithFormat:@"%d", postData.length] forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // Peform the request

        NSURLResponse *response;
        NSError *error = nil;
        NSData *receivedData = [NSURLConnection sendSynchronousRequest:request
                                                     returningResponse:&response
                                                                 error:&error];
        if (error)
        {
            NSLog(@"Handle error here.");
        }
        else
        {
            NSError *error;
            NSDictionary *jsonResult = [NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&error];
            if(error != nil)
            {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [appDelegate dismissGlobalHUD];
                });


                NSLog(@"Internet connection failed.");
            }
            else
            {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [appDelegate dismissGlobalHUD];
                });

                if([[jsonResult objectForKey:@"success"] integerValue] == 1)
                {
                    NSArray *arrayStateListLocal = [jsonResult objectForKey:@"states"];

                    self.DataRow = [[NSMutableArray alloc] init];

                    for(int i = 0; i < arrayStateListLocal.count; i++)
                    {
                        NSDictionary *dictCurrent = [arrayStateListLocal objectAtIndex:i];

                        State *objState = [[State alloc] init];
                        objState.strStateID = [dictCurrent objectForKey:@"StateId"];
                        objState.strStateName = [dictCurrent objectForKey:@"StateName"];

                        [self.DataRow addObject:objState];
                    }

                    dispatch_async(dispatch_get_main_queue(), ^{
                        [mainTableView reloadData];
                    });

                }
                else
                {
                    NSString *message = [jsonResult objectForKey:@"message"];
                    NSLog(@"message : %@",message);
                }
            }
Simon H
  • 2,495
  • 4
  • 30
  • 38
Pritesh
  • 1
  • 2
  • Possible duplicate of [AFNetworking Post Request](https://stackoverflow.com/questions/7623275/afnetworking-post-request) – iCoder4777 Jul 11 '17 at 09:40

1 Answers1

0

AFnetworking is a must for iOS Developer. Please get through AFNetworking.

Also, please go through raywenderlich tutorial for AFNetworking I am sure that you will get know how iOS client talks to webservices.

iCoder4777
  • 1,682
  • 1
  • 14
  • 35