Thursday, 19 June 2014

IOS : Save Data using Rest Web services with POST method

- (IBAction)providerRegistration:(id)sender
{

@try 
{
    [self.txtClientId resignFirstResponder];
    if([[self.txtEmrName notNull:self.txtEmrName] isEqualToString:@""] || [[self.txtUserName notNull:self.self.txtUserName] isEqualToString:@""] || [[self.txtPassword notNull:self.self.txtPassword] isEqualToString:@""]|| [[self.txtProvider notNull:self.self.txtProvider] isEqualToString:@""]|| [[self.txtClientId notNull:self.self.txtClientId] isEqualToString:@""])
    {
  UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"empty" message:@"information   required" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:Nil, nil];
        [alert show];
        return;
    }
    NSError *error;

    NSString *url = @"http://four-developers.com/service1.svc/GetData";
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    

    
    [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
      [request setHTTPMethod:@"POST"];
    
    NSMutableDictionary* mapData = [[ NSMutableDictionary alloc ] init ];
    
    //[ mapData setObject :@"MEDPRIME" forKey : @"DataSourceName"];
    [ mapData setObject :self.txtEmrName.text forKey : @"aa"];
      
    NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];
    [request setHTTPBody:postData];

    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {
                             
                               [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                   
                                  // UI Updation...
                               }];
                               
                               NSError *err = nil ;
                               NSDictionary   *responseDict = [ NSJSONSerialization JSONObjectWithData :data options : 0 error :&err];
                               NSString* strResult = [responseDict objectForKey : @"ProviderRegristrationResult" ];
                               
                               if([strResult isKindOfClass:[NSNull class]])
                               {
                                   UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"empty" message:@"Data save failed" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:Nil, nil];
                                   [alert show];
                                   return;
                               }
                               else if([strResult isEqualToString:@""])
                               {
                                   UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"empty" message:@"Data save failed" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:Nil, nil];
                                   [alert show];
                                   return;
                               }
                               else if([strResult isEqualToString:@"User Already Exist"])
                               {
                                   UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"empty" message:strResult delegate:self cancelButtonTitle:@"OK" otherButtonTitles:Nil, nil];
                                   [alert show];
                                   return;
                               }
                               else if([strResult isEqualToString:@"Success"])
                               {
                                   UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"empty" message:@"Data save successfully" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:Nil, nil];
                                   [alert show];
                                   
                               }

                           }];
    

    
}
@catch (NSException *exception)
{
    NSLog(@"%@",exception.description);
    
}

}

Saturday, 7 June 2014

Custom activityIndicator using objective C

-(void)StartProgressBar:(NSString *)strMessgae superView:(UIView *)view
{
    
    UIView *alertView=[[UIView alloc] initWithFrame:CGRectMake((view.frame.size.width/2)-50, (view.frame.size.height/2)-80, 100, 70)];
    alertView.tag=505;
    [alertView setBackgroundColor:[UIColor blackColor]];
    [alertView setAlpha:.7];
    alertView.layer.cornerRadius=8.0f;
    
    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    //[activityIndicator setFrame:CGRectMake((alertView.frame.size.width/2)-25, 10, 55, 30)]; // Commented on 28thJan2014
    [activityIndicator setFrame:CGRectMake((alertView.frame.size.width/2)-25, 20, 55, 30)];
    [activityIndicator startAnimating];
    [alertView addSubview:activityIndicator];
    
    // Commented on 28thJan2014 START --
//    UILabel *lbl=[[UILabel alloc] initWithFrame:CGRectMake(0, 40, alertView.frame.size.width, 30)];
//    lbl.text=strMessgae;
//    [lbl setBackgroundColor:[UIColor clearColor]];
//    [lbl setTextColor:[UIColor whiteColor]];
//    [lbl setFont:[UIFont fontWithName:@"Helvetica-Bold" size:11]];
//    [lbl setTextAlignment:NSTextAlignmentCenter];
//    [alertView addSubview:lbl];
    // -- END
    
    view.userInteractionEnabled = NO;
    self.navigationController.view.userInteractionEnabled = NO;
    [view addSubview:alertView];
    
    
}

-(void)StopProgressBar:(UIView *)view
{
    //[alertProgressBar dismissWithClickedButtonIndex:0 animated:YES];
    UIView *subView=(UIView *)[view viewWithTag:505];
    
    if (subView)
        [subView removeFromSuperview];
    
    view.userInteractionEnabled = YES;
    self.navigationController.view.userInteractionEnabled = YES;

}