如何在ios中使用webservices

时间:2017-07-11 09:28:32

标签: objective-c iphone

考虑各种iOS设备和方向。如何在tableview中创建webservice名称传递服务中的数据?

(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);
                }
            }

1 个答案:

答案 0 :(得分:0)

AF Developer是iOS Developer的必备工具。请完成AFNetworking.

另外,请完成raywenderlich tutorial for AFNetworking 我相信你会知道iOS客户端如何与webservices对话。