Monday, 18 December 2017

NSURLSessionDataTask and NsUrlSession manager

  @try
    {
        NSString *strurl=[NSString stringWithFormat:@"Url Here",];
        
        NSURL *url=[NSURL URLWithString:strurl];
        
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
        NSURLSession *session = [NSURLSession sharedSession];
        
        NSURLSessionDataTask *task = [session dataTaskWithRequest:request
                                                completionHandler:
                                      ^(NSData *data, NSURLResponse *response, NSError *error)
                                      {
                                          
                                          // NSError *error;
                                          NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
                                          
                                          arrResult=[[NSMutableArray alloc]init];
                                          
                                          for (NSMutableDictionary *dic in json)
                                          {
                                              [arrResult addObject:dic];
                                          }
                                          if ([arrResult count]>0)
                                          {
                                              [tblView reloadData];
                                              [MBProgressHUD hideHUDForView:self.view animated:YES];
                                          }
                                      }];
        
        [task resume];
        
    }
    @catch (NSException *exception)
    {
        NSString *strError = [NSString stringWithFormat:@"%@",exception];
        
        UIAlertView *validateAlert = [[UIAlertView alloc] initWithTitle:sharvar.strAlertMsgTitle message:strError delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [validateAlert show];

    }

No comments:

Post a Comment