Friday 11 March 2016

Pass Data from one view controller to another using App Delegate

Here we are just declare some variable as a public in Appdelegate. Then we will store some value to that variable from any class. Which will accessible to all other class.

AppDelegate.h

@property (strong, nonatomic) NSString *login_id;
@property (strong, nonatomic) NSString *login_name;

Class1.m

#import "AppDelegate.h"


In Load

AppDelegate *del = (AppDelegate*)[[UIApplication sharedApplication] delegate];
del.login_id=@"1";
del.login_name=@"MyName";

Class2.m

In btnAction or may you use RowAtIndex for TableView

AppDelegate *obj_del = (AppDelegate*)[[UIApplication sharedApplicationdelegate];
NSLog(@"%@",obj_del.login_id);
NSLog(@"%@",obj_del.login_name);

Thursday 10 March 2016

Login with WebService using SOAP

Please see Validation before implement this code



if
(_txt_id.text && _txt_pass.text.length > 0)
    {
                
        //Check Email
         if (regExMatches == 1)
         {
             NSLog(@"%@",_txt_id.text);
             str1=@"";
             str2=_txt_id.text;
             str3=_txt_pass.text;
             
             
             NSLog(@"%@",_txt_id.text);
             
         }
        //Check if number
         else if(range_number.location != NSNotFound)
         {
                str1=_txt_id.text;
                str2=@"";
                str3=_txt_pass.text;

         }
        else
         {
            NSLog(@"Enter Number or Email");
         }
        
        
     }
    else
    {
        NSLog(@"Write Something");
    }
      
    soapMessage = [NSString stringWithFormat:
                   @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                   "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
                   "<soap:Body>\n"
                   "<CheckLogin xmlns=\"http://tempuri.org/\">\n"
                   "<name1>%@</name1>\n"
                   "<name2>%@</name2>\n"
                   "<name3>%@</name3>\n"
                   "</CheckLogin>\n"
                   "</soap:Body>\n"
                   "</soap:Envelope>\n",str1,str2,str3
                   ];
    NSLog(soapMessage);
    NSURL *url = [NSURL URLWithString:@"http://192.168.1.17/abc/service.asmx"];
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
    
    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue: @"http://tempuri.org/CheckLogin" forHTTPHeaderField:@"SOAPAction"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
    
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    
    if( theConnection )
        {
        webData = [NSMutableData data];
        }
    else
        {
        NSLog(@"theConnection is NULL");
        }
    
    
    
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    
    [webData setLength: 0];
    
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    
    [webData appendData:data];
    
    
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    
    xmlParser = [[NSXMLParser alloc] initWithData: webData];
    [xmlParser setDelegate:self];
    [xmlParser setShouldResolveExternalEntities: YES];
    [xmlParser parse];
    [self someMethod];
}




#pragma mark NSXMLParserDelegate methods


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{  
    NSLog(@"%@",string);
    string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    count1++;
}
- (void)someMethod
  //This method use for navigation if login sucessfull
    if (count1>0)
    {
        Admin_Home *fac1=[self.storyboard instantiateViewControllerWithIdentifier:@"admin_home"];
        [self.navigationController pushViewController:fac1 animated:YES];
    }
    

}

Wednesday 9 March 2016

Simple example of Validation ios(Objective C)

/*I am  sharing code for validation.Here in text field you can login with email and number both with single field. So you have to check if entered value is number or email.For that i have use regular expression. */

ViewController.h


 NSRange range_number,range_email;
 NSString *regEx_number,*regEx_email;

ViewController.m

In Load

 regEx_number = @"^[0-9]{10}$";
 regEx_email=@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";

//optional and correct expression for email                                                                      
/* ^[a-z0-9](\.?[a-z0-9_-]){0,}@[a-z0-9-]+\.([a-z]{1,6}\.)?[a-z]{2,6}$*/

In ButtonAction

if (_txt_id.text && _txt_pass.text.length > 0)
    {
        //Define regular expression for Email
         NSRegularExpression *regEx = [[NSRegularExpression alloc] initWithPattern:regEx_email options:NSRegularExpressionCaseInsensitive error:nil];
        
         NSUInteger regExMatches = [regEx numberOfMatchesInString:_txt_id.text options:range:NSMakeRange(0, [_txt_id.text length])];
     
       //Define regular expression for Number
        range_number = [_txt_id.text rangeOfString:regEx_number options:NSRegularExpressionSearch];
        
//         NSLog(@"%i", regExMatches);
        
        // Check if text is Email

         if (regExMatches == 1)
         {
             NSLog(@"%@",_txt_id.text);
         
         }

        //Check if text is number

         else if(range_number.location != NSNotFound)
         {
         
              NSLog(@"Phone number is %@", [_txt_id.text substringWithRange:range_number]);
             
         }
        else
        {
            NSLog(@"Enter Only Number or Email");
        }
        
        
       
    
        //Code for navigation 
          Home_page *fac1=[self.storyboard instantiateViewControllerWithIdentifier:@"home"];
        [self.navigationController pushViewController:fac1 animated:YES];
    }
    else
    {
        NSLog(@"Write Something Text fields are blank");
    }